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.
这道题的要求是将链表中m到n位置反转。
链表处理,先加一个空表头,便于处理。然后令l指针移动到m位置前面,令r移动到m位置,这样不断把r后面的节点插入到l后面,直到n位置。
假设m=2,n=4,链表为:
head -> 1 -> 2 -> 3 -> 4 -> 5 -> NULL
加头之后为:
h -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> NULL
令l指针移动到m位置前面,令r移动到m位置,即:
h -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> NULL
^ ^
| |
l r
r后面的节点插入到l后面:
ListNode *temp = l -> next:
h -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> NULL
^ ^
| |
l temp、r
l -> next = r -> next:
|-------->|
h -> 0 -> 1 2 -> 3 -> 4 -> 5 -> NULL
^ ^
| |
l temp、r
r -> next = r -> next -> next:
|-------->|
h -> 0 -> 1 2 3 -> 4 -> 5 -> NULL
^ |-------->|
| ^
l |
temp、r
l -> next -> next = temp:
|-------->|
h -> 0 -> 1 2 <- 3 4 -> 5 -> NULL
^ |-------->|
| ^
l |
temp、r
即:
h -> 0 -> 1 -> 3 -> 2 -> 4 -> 5 -> NULL
^ ^
| |
l r
进而继续把r后面节点插入到l后面,以此类推,进行循环。。。
时间复杂度:O(n)
空间复杂度:O(1)
1 class Solution
2 {
3 public:
4 ListNode *reverseBetween(ListNode *head, int m, int n)
5 {
6 ListNode *h = new ListNode(0);
7 h -> next = head;
8
9 ListNode *l = h, *r;
10 for(int i = 1; i < m; ++ i)
11 l = l -> next;
12
13 r = l -> next;
14 for(int i = 1; i < n - m + 1; ++ i)
15 {
16 ListNode *temp = l -> next;
17 l -> next = r -> next;
18 r -> next = r -> next -> next;
19 l -> next -> next = temp;
20 }
21
22 return h -> next;
23 }
24 };