Skip to content

Commit cc875bd

Browse files
update 148
1 parent 91544dc commit cc875bd

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
import com.fishercoder.common.classes.ListNode;
44

5+
import java.util.ArrayList;
6+
import java.util.Collections;
7+
import java.util.List;
8+
59
public class _148 {
610

711
public static class Solution1 {
@@ -192,4 +196,29 @@ private ListNode getMid(ListNode head) {
192196
return mid;
193197
}
194198
}
199+
200+
public static class Solution4 {
201+
/**This is the most naive, using O(n) extra memory, O(nlogn) time.*/
202+
public ListNode sortList(ListNode head) {
203+
if (head == null) {
204+
return head;
205+
}
206+
List<Integer> list = new ArrayList<>();
207+
ListNode tmp = head;
208+
while (tmp != null) {
209+
list.add(tmp.val);
210+
tmp = tmp.next;
211+
}
212+
Collections.sort(list);
213+
ListNode pre = new ListNode(-1);
214+
ListNode newHead = new ListNode(list.get(0));
215+
pre.next = newHead;
216+
for (int i = 1; i < list.size(); i++) {
217+
ListNode next = new ListNode(list.get(i));
218+
newHead.next = next;
219+
newHead = newHead.next;
220+
}
221+
return pre.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
import com.fishercoder.common.classes.ListNode;
44
import com.fishercoder.common.utils.LinkedListUtils;
55
import com.fishercoder.solutions._148;
6-
import org.junit.BeforeClass;
7-
import org.junit.Test;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
88

9-
import static org.junit.Assert.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
1010

1111
public class _148Test {
1212
private static _148.Solution1 solution1;
1313
private static _148.Solution2 solution2;
1414
private static _148.Solution3 solution3;
15+
private static _148.Solution4 solution4;
1516
private static ListNode head;
1617
private static ListNode expected;
1718

18-
@BeforeClass
19-
public static void setup() {
19+
@BeforeEach
20+
public void setup() {
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+
public void test4() {
50+
head = LinkedListUtils.contructLinkedList(new int[]{4, 2, 1, 3});
51+
expected = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 4});
52+
assertEquals(expected, solution4.sortList(head));
53+
}
54+
4655
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy