Skip to content

Commit 2e78dc0

Browse files
committed
finish "148. Sort List". Fix #148
1 parent 08de32a commit 2e78dc0

File tree

2 files changed

+99
-6
lines changed

2 files changed

+99
-6
lines changed

README.adoc

+6-6
Original file line numberDiff line numberDiff line change
@@ -743,12 +743,12 @@
743743
//|{leetcode_base_url}/insertion-sort-list/[Insertion Sort List]
744744
//|{source_base_url}/InsertionSortList.java[Java]
745745
//|Medium
746-
//
747-
//|148
748-
//|{leetcode_base_url}/sort-list/[Sort List]
749-
//|{source_base_url}/SortList.java[Java]
750-
//|Medium
751-
//
746+
747+
|148
748+
|{leetcode_base_url}/sort-list/[Sort List]
749+
|{source_base_url}/SortList.java[Java]
750+
|Medium
751+
752752
//|149
753753
//|{leetcode_base_url}/max-points-on-a-line/[Max Points on a Line]
754754
//|{source_base_url}/MaxPointsOnALine.java[Java]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.diguage.algorithm.leetcode;
2+
3+
import com.diguage.algorithm.util.ListNode;
4+
5+
import java.util.Arrays;
6+
import java.util.Objects;
7+
8+
import static com.diguage.algorithm.util.ListNodeUtils.generate;
9+
import static com.diguage.algorithm.util.ListNodeUtils.printListNode;
10+
11+
/**
12+
* = 148. Sort List
13+
*
14+
* https://leetcode.com/problems/sort-list/[Sort List - LeetCode]
15+
*
16+
* Sort a linked list in O(n log n) time using constant space complexity.
17+
*
18+
* .Example 1:
19+
* [source]
20+
* ----
21+
* Input: 4->2->1->3
22+
* Output: 1->2->3->4
23+
* ----
24+
*
25+
* .Example 1:
26+
* [source]
27+
* ----
28+
* Input: -1->5->3->4->0
29+
* Output: -1->0->3->4->5
30+
* ----
31+
*
32+
* @author D瓜哥, https://www.diguage.com/
33+
* @since 2019-10-29 21:05
34+
*/
35+
public class SortList {
36+
37+
/**
38+
* Runtime: 4 ms, faster than 62.99% of Java online submissions for Sort List.
39+
*
40+
* Memory Usage: 40.2 MB, less than 89.47% of Java online submissions for Sort List.
41+
*/
42+
public ListNode sortList(ListNode head) {
43+
if (Objects.isNull(head) || Objects.isNull(head.next)) {
44+
return head;
45+
}
46+
ListNode prev = null;
47+
ListNode slow = head;
48+
ListNode fast = head;
49+
while (Objects.nonNull(fast) && Objects.nonNull(fast.next)) {
50+
prev = slow;
51+
slow = slow.next;
52+
fast = fast.next.next;
53+
}
54+
prev.next = null;
55+
56+
ListNode firstResult = sortList(head);
57+
ListNode secondResult = sortList(slow);
58+
59+
return merge(firstResult, secondResult);
60+
}
61+
62+
private ListNode merge(ListNode first, ListNode second) {
63+
ListNode result = new ListNode(0);
64+
ListNode tail = result;
65+
while (Objects.nonNull(first) && Objects.nonNull(second)) {
66+
if (first.val < second.val) {
67+
tail.next = first;
68+
first = first.next;
69+
} else {
70+
tail.next = second;
71+
second = second.next;
72+
}
73+
tail = tail.next;
74+
}
75+
if (Objects.isNull(first)) {
76+
tail.next = second;
77+
}
78+
if (Objects.isNull(second)) {
79+
tail.next = first;
80+
}
81+
return result.next;
82+
}
83+
84+
85+
public static void main(String[] args) {
86+
SortList solution = new SortList();
87+
88+
ListNode list1 = generate(Arrays.asList(4, 2, 1, 3));
89+
printListNode(list1);
90+
ListNode re1 = solution.sortList(list1);
91+
printListNode(re1);
92+
}
93+
}

0 commit comments

Comments
 (0)