Reverse Linked List II
来源:互联网 发布:干程序员帅哥变 编辑:程序博客网 时间:2024/04/29 01:54
题目描述如下:
Reverse a linked list from position m to n. Do it in-place and in one-pass.
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.
#define ln ListNodeclass Solution {public:ln* reverseBetween(ln* head,int m,int n){ if ( !head || !head->next || m<1 )return head; n=n-m+1;ln* guard=new ln(-1);guard->next=head;ln* pPre=guard;while(pPre&&--m){pPre=pPre->next;}//not enough m nodes;if ( pPre==NULL){delete guard;return head;}ln* pAfter=pPre->next;while(pAfter&&n--){pAfter=pAfter->next;}_reverseList(pPre,pAfter);ln* ans=guard->next;delete guard;return ans;}void _reverseList(ln*& pPre,ln* pAfter){ln* pCur=pPre->next;pPre->next=pAfter;while(pCur!=pAfter){ln* tmp=pCur->next;pCur->next=pPre->next;pPre->next=pCur;pCur=tmp;}}};
- Reverse Linked List II
- Reverse Linked List II
- Reverse Linked List II
- Reverse Linked List II
- Reverse Linked List II
- Reverse Linked List II
- Reverse Linked List II
- Reverse Linked List II
- Reverse Linked List II
- Reverse Linked List II
- Reverse Linked List II
- Reverse Linked List II
- Reverse Linked List II
- Reverse Linked List II
- Reverse Linked List II
- Reverse Linked List II
- Reverse Linked List II
- Reverse Linked List II
- c# 双缓冲 技术与例子
- wince 5.0 添加简体中文输入法 MSPY3
- Eclipse快速上手指南
- Matlab中计算程序运行时间的三种方法
- 把应用程序加载到WINDOWS CE内核的方法
- Reverse Linked List II
- __call__函数
- C#动态调用外部程序集(该程序集在dll文件中)
- WinCE BSP的BIB文件介绍
- GIT和SVN之间的五个基本区别
- linux内存分配方法
- P/NP/NP-Complete/NP-Hard Problems
- WINCE 6.0 调大image
- cocos2D权威指南的示例代码