forked from usamajay/hacktoberfest2021-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedListReverseInRange.cpp
57 lines (47 loc) · 1.46 KB
/
LinkedListReverseInRange.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
50
51
52
53
54
55
56
57
/* 92. Reverse Linked List II
Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.
Example 1:
Input: head = [1,2,3,4,5], left = 2, right = 4
Output: [1,4,3,2,5]
*/
class Solution {
public:
ListNode* th = NULL , *tt = NULL; // th -> temp head , tt-> temp tail
ListNode* reverseBetween(ListNode* head, int left, int right) {
ListNode* dum = new ListNode(-1);
dum->next = head;
ListNode* p = dum;
ListNode* curr = dum->next ;
int i = 1 ;
//ListNode th = NULL , *tt = NULL; // th -> temp head , tt-> temp tail
while(true){
// reverse zone
while(i >= left and i <= right){
ListNode* f = curr->next;
curr->next = NULL;
addFirst(curr);
curr= f ;
i++;
}
// after reverse zone
if(i>right){
p->next = th;
tt->next = curr;
break;
}
// left part before entering reverse zone
p = curr;
curr = curr->next;
i++;
}
return dum->next;
}
void addFirst(ListNode* curr){
if(th ==NULL){
th = tt = curr;
}else{
curr->next= th;
th = curr;
}
}
};