Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7462d41

Browse files
add a solution for 25
1 parentd5901a3 commit7462d41

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+
publicstaticclassSolution3 {
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+
publicListNodereverseKGroup(ListNodehead,intk) {
94+
ListNodepre =newListNode(-1);
95+
pre.next =head;
96+
ListNodetmp =pre;
97+
ListNodecurr =head;
98+
ListNode[]result =newListNode[]{null,curr};
99+
do {
100+
result =reverseKGroupHelper(result[1],k);
101+
if (result[0] ==result[1]) {
102+
tmp.next =result[0];
103+
returnpre.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+
privateListNode[]reverseKGroupHelper(ListNodehead,intk) {
114+
intoriginalK =k;
115+
ListNodetmp =head;
116+
while (tmp !=null) {
117+
tmp =tmp.next;
118+
k--;
119+
}
120+
if (k >0) {
121+
returnnewListNode[]{head,head};
122+
}else {
123+
tmp =head;
124+
k =originalK;
125+
ListNodeprev =null;
126+
while (tmp !=null) {
127+
ListNodenext =tmp.next;
128+
tmp.next =prev;
129+
prev =tmp;
130+
tmp =next;
131+
k--;
132+
if (k ==0) {
133+
returnnewListNode[]{prev,tmp};
134+
}
135+
}
136+
returnnull;
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
importstaticorg.junit.Assert.assertEquals;
1010

1111
publicclass_25Test {
12-
privatestatic_25.Solution1test;
13-
privatestatic_25.Solution2test2;
14-
privatestaticListNodeactual;
12+
privatestatic_25.Solution1solution1;
13+
privatestatic_25.Solution2solution2;
14+
privatestatic_25.Solution3solution3;
1515
privatestaticListNodeexpected;
1616
privatestaticListNodehead;
17+
privatestaticintk;
1718

1819
@BeforeClass
1920
publicstaticvoidsetup() {
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
publicvoidtest1() {
2628
head =LinkedListUtils.contructLinkedList(newint[]{1,2,3,4,5});
27-
actual =test.reverseKGroup(head,2);
29+
k =2;
2830
expected =LinkedListUtils.contructLinkedList(newint[]{2,1,4,3,5});
29-
assertEquals(actual,expected);
31+
assertEquals(expected,solution1.reverseKGroup(head,k));
3032
}
3133

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp