-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathReverseNodesInKGroup.java
102 lines (87 loc) · 2.54 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
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
package linkedlist;
// Source : https://leetcode.com/problems/reverse-nodes-in-k-group/
// Id : 25
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2021/10/29
// Topic : linkedlist
// Level : Hard
// Other :
// Tips :
// Links :
// Result : 100.00% 37.77%
public class ReverseNodesInKGroup {
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null || head.next == null || k == 1)
return head;
ListNode sentinel = new ListNode(-1, head);
ListNode start = sentinel, end = head;
while (end != null) {
int len = k;
while (len > 0 && end != null) {
end = end.next;
len--;
}
if (len == 0) {
ListNode tmp = start.next;
start.next = reverseGroup(start, end);
start = tmp;
// end = end.next;
}
}
return sentinel.next;
}
private ListNode reverseGroup(ListNode start, ListNode end) {
ListNode pre = end, cur = start.next, next;
while (cur != end) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
// practice
public ListNode reverseKGroup1(ListNode head, int k) {
// public ListNode reverseKGroup(ListNode head, int k) {
ListNode sentinel = new ListNode(-1, head);
ListNode p1 = sentinel, p2 = sentinel;
int count = 0;
while (p2.next != null) {
p2 = p2.next;
count++;
if (count == k) {
p2 = reverseGroup1(p1, p2.next);
p1 = p2;
count = 0;
}
}
return sentinel.next;
}
// sentinel & end nodes are not in the group
// return tail to be the next sentinel
private ListNode reverseGroup1(ListNode sentinel, ListNode end) {
// System.out.println(sentinel.val+" "+end.val);
ListNode newHead = end, cur = sentinel.next, tmp = null, tail = cur;
while (cur != end) {
tmp = cur.next;
cur.next = newHead;
newHead = cur;
cur = tmp;
}
sentinel.next = newHead;
return tail;
}
class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
}