-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathReverseNodesInKGroup.java
44 lines (37 loc) · 1.12 KB
/
ReverseNodesInKGroup.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
41
42
43
44
// https://leetcode.com/problems/reverse-nodes-in-k-group
// T: O(N)
// S: O(1)
public class ReverseNodesInKGroup {
public static ListNode reverseKGroup(ListNode head, int k) {
if (head == null || head.next == null || k == 1) {
return head;
}
final ListNode result = new ListNode(0);
result.next = head;
for (ListNode i = result ; i != null ; ) {
ListNode start = i;
// go forward k steps
int count = 0;
for ( ; count < k && i != null ; count++) {
i = i.next;
}
if (count == k && i != null) {
i = reverse(start, i);
}
}
return result.next;
}
private static ListNode reverse(ListNode start, ListNode end) {
ListNode a = start.next, b = start.next.next, terminal = end.next;
a.next = end.next;
while (b != terminal) {
ListNode c = b.next;
b.next = a;
a = b;
b = c;
}
ListNode newStart = start.next;
start.next = a;
return newStart;
}
}