-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeetCode25.java
84 lines (75 loc) · 2.38 KB
/
LeetCode25.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
public class LeetCode25 {
private boolean found = false;
public static void main(String[] args) {
LeetCode25 leetcode = new LeetCode25();
ListNode l = new ListNode(1);
l.next = new ListNode(2);
l.next.next = new ListNode(3);
l.next.next.next = new ListNode(4);
l.next.next.next.next = new ListNode(5);
l.next.next.next.next.next = new ListNode(6);
l.next.next.next.next.next.next = new ListNode(7);
l.next.next.next.next.next.next.next = new ListNode(8);
l.next.next.next.next.next.next.next.next = new ListNode(9);
l.next.next.next.next.next.next.next.next.next = new ListNode(10);
l.next.next.next.next.next.next.next.next.next.next = new ListNode(11);
System.out.println("Result: " + leetcode.reverseKGroup(l, 4));
}
public ListNode reverseKGroup(ListNode head, int k) {
if (k == 1) {
return head;
}
ListNode checkpoint = head;
ListNode end = null;
ListNode begin = head;
int counter = 0;
while (head != null) {
if (counter == k) {
checkpoint.next = reverseKGroup(head, k);
head = null;
counter = 0;
} else {
begin = head;
head = head.next;
begin.next = end;
end = begin;
counter++;
}
}
if (counter != k && checkpoint != null && found) {
checkpoint.next = (reverse(checkpoint.next));
found = false;
}
if (counter != k && checkpoint != null && checkpoint.next == null) {
found = true;
}
return begin;
}
private ListNode reverse(ListNode head) {
ListNode prev = null;
ListNode cur = head;
while (head != null) {
cur = head;
head = head.next;
cur.next = prev;
prev = cur;
}
return cur;
}
public static class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
public String toString() {
return next != null ? val + "->" + next : val + "";
}
}
}