[LeetCode] Reverse Linked List II
2014-06-20 09:58
316 查看
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.
方法一:我先写了个简单的reverseList,然后基于reverseList,要找到pre_m, post_n, 然后断开连接,重新连接即可。
吐槽一下LeetCode,竟然是基于打印检测结果,我的程序中有些打印语句,死活过不了,看来半天,没找出问题,去掉打印语句后,就没问题了。。
上code,唯一注意的一点是link是从1 开始的,所以 pre_m 是第m-1个,跳出while循环时,p指向的是第n个,post_n就是p->next.
另外,为了方便出来m=1的情况,加了个dummy的空Node,省去了一大堆判断,是个好方法。。
方法二:不用reverseList,直接原地reverse,注意处理好pre_m 的找法。。
方法三: 方法二中寻找pre_m的方法略微麻烦,有更好的方法,dummy节点的index是0,所以,可以利用这一点去寻找pre_m,下面的代码中p完全可以不要,不过为了清楚,
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.
方法一:我先写了个简单的reverseList,然后基于reverseList,要找到pre_m, post_n, 然后断开连接,重新连接即可。
吐槽一下LeetCode,竟然是基于打印检测结果,我的程序中有些打印语句,死活过不了,看来半天,没找出问题,去掉打印语句后,就没问题了。。
上code,唯一注意的一点是link是从1 开始的,所以 pre_m 是第m-1个,跳出while循环时,p指向的是第n个,post_n就是p->next.
另外,为了方便出来m=1的情况,加了个dummy的空Node,省去了一大堆判断,是个好方法。。
ListNode * reverseList(ListNode* head) { if(head == NULL) return NULL; ListNode *pre = NULL; ListNode *cur = head; ListNode *next = NULL; while(cur) { next = cur->next; cur->next = pre; pre = cur; cur = next; } return pre; } class Solution { public: ListNode *reverseBetween(ListNode *head, int m, int n) { ListNode dummy(100); dummy.next = head; ListNode *pre_m = &dummy; ListNode *post_n = NULL; ListNode *p = head; int cnt = 1; while(cnt < n) { if(cnt == (m-1)) pre_m = p; if(p) p = p->next; cnt++; } post_n = p->next; // build a signle link, and call reverseList p->next = NULL; //store m node in variable p; p = pre_m->next; pre_m->next = reverseList(pre_m->next); // connect pre_m and n p->next = post_n; //connect m and post_n return dummy.next; } };
方法二:不用reverseList,直接原地reverse,注意处理好pre_m 的找法。。
class Solution { public: ListNode *reverseBetween(ListNode *head, int m, int n) { ListNode dummy(-1); dummy.next = head; ListNode *pre_m = &dummy; ListNode *p = head; int cnt = 1; for(; cnt < m; cnt ++) { if(cnt == (m-1)) pre_m = p; p = p->next; } //now p point to m ListNode *pre = NULL; ListNode *cur = p; ListNode *next = NULL; for( cnt = m ; cnt <= n; cnt ++) { next = cur->next; cur->next = pre; pre = cur; cur = next; } // now pre points to n; // now cur points to post_n; pre_m ->next = pre; p->next = cur; return dummy.next; } };
方法三: 方法二中寻找pre_m的方法略微麻烦,有更好的方法,dummy节点的index是0,所以,可以利用这一点去寻找pre_m,下面的代码中p完全可以不要,不过为了清楚,
class Solution { public: ListNode *reverseBetween(ListNode *head, int m, int n) { ListNode dummy(-1); dummy.next = head; ListNode *pre_m = &dummy; ListNode *p = head; int cnt = 0; for(; cnt < m-1; cnt ++) { pre_m = pre_m->next; } //now pre_m point to m-1; p = pre_m->next; //now p point to m ListNode *pre = NULL; ListNode *cur = p; ListNode *next = NULL; for( cnt = m ; cnt <= n; cnt ++) { next = cur->next; cur->next = pre; pre = cur; cur = next; } // now pre points to n; // now cur points to post_n; pre_m ->next = pre; p->next = cur; return dummy.next; } };
相关文章推荐
- leetcode 41: Reverse Linked List II
- leetcode 76: Reverse Linked List II
- [leetcode] Reverse Linked List II
- LeetCode_Reverse Linked List II
- LeetCode-Reverse Linked List II
- leetcode 41: Reverse Linked List II
- LeetCode:Reverse Linked List II
- LeetCode Online Judge 题目C# 练习 - Reverse Linked List II
- leetcode -- Reverse Linked List II
- [Leetcode]Reverse Linked List II
- [LeetCode] Reverse Linked List II
- [LeetCode] Reverse Linked List II 解题报告
- [Leetcode] Reverse Linked List II
- leetcode--Reverse Linked List II
- [LeetCode]Reverse Linked List II
- leetcode 76: Reverse Linked List II
- LeetCode —— Reverse Linked List II
- [leetcode]Reverse Linked List II
- [LeetCode] Reverse Linked List II
- LeetCode: Reverse Linked List II