Skip to content

Commit 7462d41

Browse files
add a solution for 25
1 parent d5901a3 commit 7462d41

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

src/main/java/com/fishercoder/solutions/_25.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,57 @@ public ListNode reverseKGroup(ListNode head, int k) {
8484
}
8585
}
8686

87+
public static class Solution3 {
88+
/**
89+
* My completely original solution on 10/25/2021. Beats 100% submissions on LeetCode in runtime.
90+
* Again, using a pen and paper to visualize the process helps a lot!
91+
* My helper function returns two nodes: reversed node head and the starting node for the next reversal.
92+
*/
93+
public ListNode reverseKGroup(ListNode head, int k) {
94+
ListNode pre = new ListNode(-1);
95+
pre.next = head;
96+
ListNode tmp = pre;
97+
ListNode curr = head;
98+
ListNode[] result = new ListNode[]{null, curr};
99+
do {
100+
result = reverseKGroupHelper(result[1], k);
101+
if (result[0] == result[1]) {
102+
tmp.next = result[0];
103+
return pre.next;
104+
} else {
105+
tmp.next = result[0];
106+
while (tmp.next != null) {
107+
tmp = tmp.next;
108+
}
109+
}
110+
} while (true);
111+
}
112+
113+
private ListNode[] reverseKGroupHelper(ListNode head, int k) {
114+
int originalK = k;
115+
ListNode tmp = head;
116+
while (tmp != null) {
117+
tmp = tmp.next;
118+
k--;
119+
}
120+
if (k > 0) {
121+
return new ListNode[]{head, head};
122+
} else {
123+
tmp = head;
124+
k = originalK;
125+
ListNode prev = null;
126+
while (tmp != null) {
127+
ListNode next = tmp.next;
128+
tmp.next = prev;
129+
prev = tmp;
130+
tmp = next;
131+
k--;
132+
if (k == 0) {
133+
return new ListNode[]{prev, tmp};
134+
}
135+
}
136+
return null;
137+
}
138+
}
139+
}
87140
}

src/test/java/com/fishercoder/_25Test.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,39 @@
99
import static org.junit.Assert.assertEquals;
1010

1111
public class _25Test {
12-
private static _25.Solution1 test;
13-
private static _25.Solution2 test2;
14-
private static ListNode actual;
12+
private static _25.Solution1 solution1;
13+
private static _25.Solution2 solution2;
14+
private static _25.Solution3 solution3;
1515
private static ListNode expected;
1616
private static ListNode head;
17+
private static int k;
1718

1819
@BeforeClass
1920
public static void setup() {
20-
test = new _25.Solution1();
21-
test2 = new _25.Solution2();
21+
solution1 = new _25.Solution1();
22+
solution2 = new _25.Solution2();
23+
solution3 = new _25.Solution3();
2224
}
2325

2426
@Test
2527
public void test1() {
2628
head = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 4, 5});
27-
actual = test.reverseKGroup(head, 2);
29+
k = 2;
2830
expected = LinkedListUtils.contructLinkedList(new int[]{2, 1, 4, 3, 5});
29-
assertEquals(actual, expected);
31+
assertEquals(expected, solution1.reverseKGroup(head, k));
3032
}
3133

3234
@Test
3335
public void test2() {
3436
head = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 4, 5, 6, 7});
35-
actual = test2.reverseKGroup(head, 4);
3637
expected = LinkedListUtils.contructLinkedList(new int[]{4, 3, 2, 1, 5, 6, 7});
37-
assertEquals(actual, expected);
38+
assertEquals(expected, solution2.reverseKGroup(head, 4));
39+
}
40+
41+
@Test
42+
public void test3() {
43+
head = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 4, 5, 6, 7});
44+
expected = LinkedListUtils.contructLinkedList(new int[]{4, 3, 2, 1, 5, 6, 7});
45+
assertEquals(expected, solution3.reverseKGroup(head, 4));
3846
}
3947
}

0 commit comments

Comments
 (0)