-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-92-Reverse-Linked-List-II.java
More file actions
122 lines (97 loc) · 3.3 KB
/
LeetCode-92-Reverse-Linked-List-II.java
File metadata and controls
122 lines (97 loc) · 3.3 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
LeetCode: https://leetcode.com/problems/reverse-linked-list-ii/
LintCode: http://www.lintcode.com/problem/reverse-linked-list-ii/
JiuZhang: http://www.jiuzhang.com/solutions/reverse-linked-list-ii/
ProgramCreek: http://www.programcreek.com/2014/06/leetcode-reverse-linked-list-ii-java/
Analysis:
Use four pointers.
prev1 is m-1, curr1 is m, prev2 is n, curr is n+1.
Reverse nodes [m, n]
*/
/**
* 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 {
// 1.
// public ListNode reverseBetween(ListNode head, int m, int n) {
// if(head == null || m >= n) return head;
// ListNode dummy = new ListNode(0);
// dummy.next = head;
// ListNode prev1 = dummy, curr1 = head, prev2 = dummy, curr2 = head;
// int count = 1;
// while(count <= n){
// ListNode next = curr2.next;
// if(count == m){
// prev1 = prev2;
// curr1 = curr2;
// }
// if(count >= m){
// curr2.next = prev2;
// }
// prev2 = curr2;
// curr2 = next;
// count++;
// }
// prev1.next = prev2;
// curr1.next = curr2;
// return dummy.next;
// }
// 2.
/*
Similar to this one: https://leetcode.com/problems/reverse-nodes-in-k-group/
*/ public ListNode reverseBetween(ListNode head, int left, int right) {
if (head == null || head.next == null) return head;
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode prev = dummy;
for (int i = 1; i <= left - 1; i++) {
prev = prev.next;
}
ListNode next = dummy;
for (int i = 1; i <= right + 1; i++) {
next = next.next;
}
reverse(prev, next);
return dummy.next;
}
// prev -> last -> curr -> ...... -> kthNode -> next
private ListNode reverse(ListNode prev, ListNode next) {
ListNode last = prev.next;
ListNode curr = prev.next.next;
while (curr != next) {
last.next = curr.next;
curr.next = prev.next;
prev.next = curr;
curr = last.next;
}
return last;
}
// 3. Similar to reverse next K nodes
public ListNode reverseBetween(ListNode head, int left, int right) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode prev = dummy;
for (int i = 0; i < left - 1; i++) {
prev = prev.next;
}
// reverse next (right - left + 1) nodes
int len = right - left + 1;
ListNode curr = prev.next;
ListNode last = prev.next;
for (int i = 0; i < len && curr != null; i++) {
ListNode next = curr.next;
curr.next = prev.next;
prev.next = curr;
curr = next;
}
last.next = curr;
return dummy.next;
}
}