-
Notifications
You must be signed in to change notification settings - Fork 0
/
0092_Reverse_Linked_List_II.java
40 lines (35 loc) · 1.27 KB
/
0092_Reverse_Linked_List_II.java
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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseBetween(ListNode head, int left, int right) {
ListNode dummyHead = new ListNode(0, head); // points to head
ListNode current = dummyHead;
// advance past the first (left - 2) nodes (-> the last unreversed node)
for(int i = 0; i < left - 1; i++) current = current.next;
ListNode lastUnreversedNode = current;
// advance one more (current -> first reversed node)
current = current.next;
ListNode reversedTail = current;
// reverse (left - right + 1) nodes
ListNode reversedHead = null, temp;
// reverse the sublist
for(int i = left; i <= right; i++) {
temp = current;
current = current.next;
temp.next = reversedHead;
reversedHead = temp;
}
// re-attach the reversed list
lastUnreversedNode.next = reversedHead;
reversedTail.next = current;
return dummyHead.next;
}
}