leetcode 092 Reverse Linked List II
2016-05-14 10:50
459 查看
Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given
return
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
Subscribe to see which companies asked this question
For example:
Given
1->2->3->4->5->NULL, m = 2 and n = 4,
return
1->4->3->2->5->NULL.
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
Subscribe to see which companies asked this question
class Solution { public: ListNode* reverseBetween(ListNode* head, int m, int n) { ListNode *lm=head, *pre=NULL, *ln=head, *temp=head; for(int i=1; i<=n; i++) { if(i < m) { pre=lm; lm=pre->next; } ln=temp; temp=temp->next; } ListNode *next=ln->next; for(int i=m; i<=n; i++) { ListNode *lm_next = lm->next; lm->next=next; next = lm; lm = lm_next; } if(pre!=NULL) { pre->next=next; } else { head = next; } return head; } }
相关文章推荐
- word2vec (二) CBOW
- java-反射初学(2)
- Move Zeroes
- java中包及修饰符的研究与应用
- Eclipse 插件安装方法和插件加载失败解决办法
- Android大神的博客
- hdu 5677-ztr loves substring
- DP————1003
- hive介绍
- mysql工作中常见问题
- laravel框架学习笔记--阿里云部署
- Java 回调机制
- sdibt 1244 烦人的幻灯片
- 第一章 Shiro简介——《跟我学Shiro》
- 【HDU】 4309 Seikimatsu Occult Tonneru
- sdibt 1244 烦人的幻灯片
- php 可变变量
- magento 列表页显示产品属性值的几种调用方式
- 【bzoj1149】 [CTSC2007]风玲Mobiles
- openjudge7624 山区建小学