-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeetCode-148-Sort-List.java
109 lines (90 loc) · 2.91 KB
/
LeetCode-148-Sort-List.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
// 1. Merge Sort
/**
https://leetcode.com/problems/sort-list/discuss/46714/Java-merge-sort-solution
*/
public ListNode sortList(ListNode head) {
if (head == null || head.next == null) return head;
// step 1: cut the list to two halves
ListNode prev = null, slow = head, fast = head;
while (fast != null && fast.next != null) {
prev = slow;
slow = slow.next;
fast = fast.next.next;
}
prev.next = null;
// step 2: sort the two lists
ListNode p1 = sortList(head);
ListNode p2 = sortList(slow);
// step 3: merge the two lists
return merge(p1, p2);
}
private ListNode merge(ListNode p1, ListNode p2) {
ListNode dummy = new ListNode(-1);
ListNode prev = dummy;
while (p1 != null && p2 != null) {
if (p1.val < p2.val) {
prev.next = p1;
p1 = p1.next;
} else {
prev.next = p2;
p2 = p2.next;
}
prev = prev.next;
}
if (p1 != null) {
prev.next = p1;
}
if (p2 != null) {
prev.next = p2;
}
return dummy.next;
}
// Another way to split the list into two halves
public ListNode sortList(ListNode head) {
if (head == null || head.next == null) return head;
// ListNode prev = null, slow = head, fast = head;
// while (fast != null && fast.next != null) {
// prev = slow;
// slow = slow.next;
// fast = fast.next.next;
// }
ListNode slow = head, fast = head.next;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
fast = slow.next;
slow.next = null;
ListNode left = sortList(head);
ListNode right = sortList(fast);
return merge(left, right);
}
private ListNode merge(ListNode left, ListNode right) {
ListNode dummy = new ListNode(-1);
ListNode curr = dummy;
while (left != null && right != null) {
if (left.val < right.val) {
curr.next = left;
left = left.next;
} else {
curr.next = right;
right = right.next;
}
curr = curr.next;
}
if (left != null) curr.next = left;
if (right != null) curr.next = right;
return dummy.next;
}
}