Skip to content

Commit 4036244

Browse files
add a solution for 148
1 parent 06e8e30 commit 4036244

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
return cnt;
141141
}
142142
}
143+
144+
public static class Solution3 {
145+
/**
146+
* Credit: https://leetcode.com/problems/sort-list/solution/ top down approach.
147+
*/
148+
public ListNode sortList(ListNode head) {
149+
if (head == null || head.next == null) {
150+
return head;
151+
}
152+
ListNode mid = getMid(head);
153+
ListNode left = sortList(head);
154+
ListNode right = sortList(mid);
155+
return mergeList(left, right);
156+
}
157+
158+
private ListNode mergeList(ListNode left, ListNode right) {
159+
ListNode pre = new ListNode(-1);
160+
ListNode tmp = 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+
} else if (right != null) {
174+
tmp.next = right;
175+
}
176+
return pre.next;
177+
}
178+
179+
private ListNode getMid(ListNode head) {
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+
ListNode midPrev = null;
186+
while (head != null && head.next != null) {
187+
midPrev = (midPrev == null) ? head : midPrev.next;
188+
head = head.next.next;
189+
}
190+
ListNode mid = midPrev.next;
191+
midPrev.next = null;//this is the key, otherwise, StackOverflow exception will occur.
192+
return mid;
193+
}
194+
}
143195
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.ListNode;
4+
import com.fishercoder.common.utils.LinkedListUtils;
5+
import com.fishercoder.solutions._148;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class _148Test {
12+
private static _148.Solution1 solution1;
13+
private static _148.Solution2 solution2;
14+
private static _148.Solution3 solution3;
15+
private static ListNode head;
16+
private static ListNode expected;
17+
18+
@BeforeClass
19+
public static void setup() {
20+
solution1 = new _148.Solution1();
21+
solution2 = new _148.Solution2();
22+
solution3 = new _148.Solution3();
23+
}
24+
25+
@Test
26+
public void test1() {
27+
head = LinkedListUtils.contructLinkedList(new int[]{4, 2, 1, 3});
28+
expected = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 4});
29+
assertEquals(expected, solution1.sortList(head));
30+
}
31+
32+
@Test
33+
public void test2() {
34+
head = LinkedListUtils.contructLinkedList(new int[]{4, 2, 1, 3});
35+
expected = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 4});
36+
assertEquals(expected, solution2.sortList(head));
37+
}
38+
39+
@Test
40+
public void test3() {
41+
head = LinkedListUtils.contructLinkedList(new int[]{4, 2, 1, 3});
42+
expected = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 4});
43+
assertEquals(expected, solution3.sortList(head));
44+
}
45+
46+
}

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