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

Commitcc875bd

Browse files
update 148
1 parent91544dc commitcc875bd

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
importcom.fishercoder.common.classes.ListNode;
44

5+
importjava.util.ArrayList;
6+
importjava.util.Collections;
7+
importjava.util.List;
8+
59
publicclass_148 {
610

711
publicstaticclassSolution1 {
@@ -192,4 +196,29 @@ private ListNode getMid(ListNode head) {
192196
returnmid;
193197
}
194198
}
199+
200+
publicstaticclassSolution4 {
201+
/**This is the most naive, using O(n) extra memory, O(nlogn) time.*/
202+
publicListNodesortList(ListNodehead) {
203+
if (head ==null) {
204+
returnhead;
205+
}
206+
List<Integer>list =newArrayList<>();
207+
ListNodetmp =head;
208+
while (tmp !=null) {
209+
list.add(tmp.val);
210+
tmp =tmp.next;
211+
}
212+
Collections.sort(list);
213+
ListNodepre =newListNode(-1);
214+
ListNodenewHead =newListNode(list.get(0));
215+
pre.next =newHead;
216+
for (inti =1;i <list.size();i++) {
217+
ListNodenext =newListNode(list.get(i));
218+
newHead.next =next;
219+
newHead =newHead.next;
220+
}
221+
returnpre.next;
222+
}
223+
}
195224
}

‎src/test/java/com/fishercoder/_148Test.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,25 @@
33
importcom.fishercoder.common.classes.ListNode;
44
importcom.fishercoder.common.utils.LinkedListUtils;
55
importcom.fishercoder.solutions._148;
6-
importorg.junit.BeforeClass;
7-
importorg.junit.Test;
6+
importorg.junit.jupiter.api.BeforeEach;
7+
importorg.junit.jupiter.api.Test;
88

9-
importstaticorg.junit.Assert.assertEquals;
9+
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
1010

1111
publicclass_148Test {
1212
privatestatic_148.Solution1solution1;
1313
privatestatic_148.Solution2solution2;
1414
privatestatic_148.Solution3solution3;
15+
privatestatic_148.Solution4solution4;
1516
privatestaticListNodehead;
1617
privatestaticListNodeexpected;
1718

18-
@BeforeClass
19-
publicstaticvoidsetup() {
19+
@BeforeEach
20+
publicvoidsetup() {
2021
solution1 =new_148.Solution1();
2122
solution2 =new_148.Solution2();
2223
solution3 =new_148.Solution3();
24+
solution4 =new_148.Solution4();
2325
}
2426

2527
@Test
@@ -43,4 +45,11 @@ public void test3() {
4345
assertEquals(expected,solution3.sortList(head));
4446
}
4547

48+
@Test
49+
publicvoidtest4() {
50+
head =LinkedListUtils.contructLinkedList(newint[]{4,2,1,3});
51+
expected =LinkedListUtils.contructLinkedList(newint[]{1,2,3,4});
52+
assertEquals(expected,solution4.sortList(head));
53+
}
54+
4655
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp