-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
92. Reverse Linked List II
59 lines (53 loc) · 1.48 KB
/
92. Reverse Linked List II
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
50
51
52
53
54
55
56
57
58
59
//T.c -O(n)
//s.C -O(1)
// function used to reverse a linked list
ListNode* reverse(ListNode* head)
{
ListNode* prev = NULL;
ListNode* curr = head;
while (curr) {
ListNode* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
ListNode* reverseBetween(ListNode* head, int left, int right) {
// function used to reverse a linked list from position m to n
if (left == right)
return head;
// revs and revend is start and end respectively of the
// portion of the linked list which need to be reversed.
// revs_prev is previous of starting position and
// revend_next is next of end of list to be reversed.
ListNode*revs = NULL, *revs_prev = NULL;
ListNode*revend = NULL, *revend_next = NULL;
// Find values of above pointers.
int i = 1;
ListNode* curr = head;
while (curr && i <= right) {
if (i < left)
revs_prev = curr;
if (i == left)
revs = curr;
if (i == right) {
revend = curr;
revend_next = curr->next;
}
curr = curr->next;
i++;
}
revend->next = NULL;
// Reverse linked list starting with revs.
revend = reverse(revs);
// If starting position was not head
if (revs_prev)
revs_prev->next = revend;
// If starting position was head
else
head = revend;
revs->next = revend_next;
return head;
}
};