-
Notifications
You must be signed in to change notification settings - Fork 3
/
lc148.java
59 lines (58 loc) · 1.45 KB
/
lc148.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
// Sort a linked list in O(n log n) time using constant space complexity.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode sortList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode median = findMedian(head);
ListNode left = sortList(head);
ListNode right = sortList(median);
return mergeList(left, right);
}
private ListNode findMedian(ListNode head){
ListNode p1 = new ListNode(0);
p1.next = head;
ListNode p2 = p1;
while(p2.next != null && p2.next.next != null){
p2 = p2.next.next;
p1 = p1.next;
}
p2 = p1.next;
p1.next = null;
return p2;
}
private ListNode mergeList(ListNode p1, ListNode p2){
ListNode dummy = new ListNode(0);
ListNode res = dummy;
while(p1!=null || p2!=null)
{
if (p1 != null && p2 != null)
{
if (p1.val < p2.val){
dummy.next = p1;
p1 = p1.next;
dummy = dummy.next;
}
else {
dummy.next = p2;
p2 = p2.next;
dummy = dummy.next;
}
}
else if (p1 != null)
{
dummy.next = p1; break;
}
else {
dummy.next = p2; break;
}
}
return res.next;
}
}