92. Reverse Linked List II (List)
2015-10-03 10:36
351 查看
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.
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.
class Solution { public: ListNode *reverseBetween(ListNode *head, int m, int n) { if(!head || m==n ) return head; ListNode * lastOfPrePart = head; ListNode * preReverseNode; ListNode * firstPostPart; ListNode * nextReverseNode; ListNode * currentReverseNode; if(m>1) { for(int i = 0; i < m-2; i++) { lastOfPrePart = lastOfPrePart->next; } currentReverseNode = lastOfPrePart->next; } else { currentReverseNode = head; } nextReverseNode = currentReverseNode->next; firstPostPart = currentReverseNode; for(int i = m; i < n+1; i++ ) { firstPostPart = firstPostPart -> next; } currentReverseNode ->next = firstPostPart; while(nextReverseNode!= firstPostPart) { preReverseNode = currentReverseNode; currentReverseNode = nextReverseNode; nextReverseNode = currentReverseNode->next; currentReverseNode->next = preReverseNode; } if(m!=1) { lastOfPrePart->next = currentReverseNode; } else { head = currentReverseNode; } return head; } };
相关文章推荐
- 顺序表的实现C++封装
- [Qt] QString 和 char* 转换
- DOM一致性检测
- matlab+opencv
- HDU 2689 Sort it(逆序对-BIT)
- 四则运算
- 2-SAT
- jsp 百度编辑器配置 整合springmvc注意细节
- 一些安卓开源框架整理
- 一些安卓开源框架整理
- <01分数规划>淬炼神体
- [Qt] qtcreator 中打开console
- 图的邻接矩阵
- 判断圆与三角形是否有交点
- [经济学原理|政治部分]劳动价值理论
- 1.3.3 打开A20,实现32位寻址
- Linux中的查看已挂载的文件系统命令
- 图的邻接链表
- HDU 2520 我是菜鸟,我怕谁(水~)
- 字符函数lpad,rpad,ltrim,rtrim,substr使用