forked from kamyu104/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse-linked-list-ii.cpp
49 lines (42 loc) · 1.13 KB
/
reverse-linked-list-ii.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Time: O(n)
// Space: O(1)
/**
* Definition of singly-linked-list:
*
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param head: The head of linked list.
* @param m: The start position need to reverse.
* @param n: The end position need to reverse.
* @return: The new head of partial reversed linked list.
*/
ListNode *reverseBetween(ListNode *head, int m, int n) {
ListNode dummy(INT_MIN);
dummy.next = head;
ListNode *prev = &dummy;
for (int i = 0; i < m - 1; ++i) {
prev = prev->next;
}
ListNode *head2 = prev;
prev = prev->next;
ListNode *cur = prev->next;
for (int i = m; i < n; ++i) {
prev->next = cur->next; // remove cur from the list
cur->next = head2->next; // add cur to the head
head2->next = cur; // add cur to the head
cur = prev->next; // get next cur
}
return dummy.next;
}
};