forked from Alex-Keyes/InterviewBit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreverseBetween.cpp
44 lines (37 loc) · 1.21 KB
/
reverseBetween.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
class Solution {
public:
// Reverses the linkedList which starts from head, and extends to size nodes.
// Returns the end node.
// Also sets the head->next as endNode->next.
ListNode *reverseLinkedList(ListNode *head, int size) {
if (size <= 1) return head;
ListNode *cur = head, *nextNode = head->next, *tmp;
for (int i = 0; i < (size - 1); i++) {
tmp = nextNode->next;
nextNode->next = cur;
cur = nextNode;
nextNode = tmp;
}
head->next = nextNode;
return cur;
}
ListNode *reverseBetween(ListNode *head, int m, int n) {
// Introduce dummyhead to not handle corner cases.
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;
// Figure out the start node of the sublist we are going to reverse
ListNode* prev = dummyHead;
ListNode* cur = head;
int index = 1;
while (index < m) {
prev = cur;
cur = cur->next;
index++;
}
// At this point, we have start of sublist in cur, prev of startSubList in prev.
// Lets reverse the sublist now.
ListNode* endSubList = reverseLinkedList(cur, n - m + 1);
prev->next = endSubList;
return dummyHead->next;
}
};