-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSublistReversalproblem
More file actions
29 lines (24 loc) · 870 Bytes
/
SublistReversalproblem
File metadata and controls
29 lines (24 loc) · 870 Bytes
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
class Solution {
public ListNode reverseBetween(ListNode head, int start, int end) {
if (start == end) {
return head;
}
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
ListNode previousNodeBeforeReversal = dummyHead;
int position = 1;
while (position < start) {
previousNodeBeforeReversal = previousNodeBeforeReversal.next;
position++;
}
ListNode subListPointer = previousNodeBeforeReversal.next;
while (start < end) {
ListNode comingToTheFront = subListPointer.next;
subListPointer.next = comingToTheFront.next;
comingToTheFront.next = previousNodeBeforeReversal;
previousNodeBeforeReversal.next = comingToTheFront;
start++;
}
return dummyHead.next;
}
}