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

Commit4036244

Browse files
add a solution for 148
1 parent06e8e30 commit4036244

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

‎src/main/java/com/fishercoder/solutions/_148.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,56 @@ int getCount(ListNode head) {
140140
returncnt;
141141
}
142142
}
143+
144+
publicstaticclassSolution3 {
145+
/**
146+
* Credit: https://leetcode.com/problems/sort-list/solution/ top down approach.
147+
*/
148+
publicListNodesortList(ListNodehead) {
149+
if (head ==null ||head.next ==null) {
150+
returnhead;
151+
}
152+
ListNodemid =getMid(head);
153+
ListNodeleft =sortList(head);
154+
ListNoderight =sortList(mid);
155+
returnmergeList(left,right);
156+
}
157+
158+
privateListNodemergeList(ListNodeleft,ListNoderight) {
159+
ListNodepre =newListNode(-1);
160+
ListNodetmp =pre;
161+
while (left !=null &&right !=null) {
162+
if (left.val <right.val) {
163+
tmp.next =left;
164+
left =left.next;
165+
}else {
166+
tmp.next =right;
167+
right =right.next;
168+
}
169+
tmp =tmp.next;
170+
}
171+
if (left !=null) {
172+
tmp.next =left;
173+
}elseif (right !=null) {
174+
tmp.next =right;
175+
}
176+
returnpre.next;
177+
}
178+
179+
privateListNodegetMid(ListNodehead) {
180+
/**The key/trick is in this method:
181+
* it directly uses this head to iterate, so that we could use this top down recursive approach.
182+
* If we assign head to slow and fast pointers, then this algorithm will run into StackOverflow exception.
183+
*
184+
* This is an absolutely amazing method!*/
185+
ListNodemidPrev =null;
186+
while (head !=null &&head.next !=null) {
187+
midPrev = (midPrev ==null) ?head :midPrev.next;
188+
head =head.next.next;
189+
}
190+
ListNodemid =midPrev.next;
191+
midPrev.next =null;//this is the key, otherwise, StackOverflow exception will occur.
192+
returnmid;
193+
}
194+
}
143195
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.classes.ListNode;
4+
importcom.fishercoder.common.utils.LinkedListUtils;
5+
importcom.fishercoder.solutions._148;
6+
importorg.junit.BeforeClass;
7+
importorg.junit.Test;
8+
9+
importstaticorg.junit.Assert.assertEquals;
10+
11+
publicclass_148Test {
12+
privatestatic_148.Solution1solution1;
13+
privatestatic_148.Solution2solution2;
14+
privatestatic_148.Solution3solution3;
15+
privatestaticListNodehead;
16+
privatestaticListNodeexpected;
17+
18+
@BeforeClass
19+
publicstaticvoidsetup() {
20+
solution1 =new_148.Solution1();
21+
solution2 =new_148.Solution2();
22+
solution3 =new_148.Solution3();
23+
}
24+
25+
@Test
26+
publicvoidtest1() {
27+
head =LinkedListUtils.contructLinkedList(newint[]{4,2,1,3});
28+
expected =LinkedListUtils.contructLinkedList(newint[]{1,2,3,4});
29+
assertEquals(expected,solution1.sortList(head));
30+
}
31+
32+
@Test
33+
publicvoidtest2() {
34+
head =LinkedListUtils.contructLinkedList(newint[]{4,2,1,3});
35+
expected =LinkedListUtils.contructLinkedList(newint[]{1,2,3,4});
36+
assertEquals(expected,solution2.sortList(head));
37+
}
38+
39+
@Test
40+
publicvoidtest3() {
41+
head =LinkedListUtils.contructLinkedList(newint[]{4,2,1,3});
42+
expected =LinkedListUtils.contructLinkedList(newint[]{1,2,3,4});
43+
assertEquals(expected,solution3.sortList(head));
44+
}
45+
46+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp