-
Notifications
You must be signed in to change notification settings - Fork 91
/
SortList148.java
55 lines (48 loc) · 1.27 KB
/
SortList148.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
/**
* 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 SortList148 {
public ListNode sortList(ListNode head) {
if (head == null) return null;
if (head.next == null) return head;
ListNode s = head;
ListNode f = head;
ListNode pre = s;
while (s != null && f != null && f.next != null) {
pre = s;
s = s.next;
f = f.next.next;
}
pre.next = null;
ListNode left = sortList(head);
ListNode right = sortList(s);
return merge(left, right);
}
private ListNode merge(ListNode left, ListNode right) {
ListNode res = new ListNode(-1);
ListNode p = res;
ListNode l = left;
ListNode r = right;
while (l != null && r != null) {
if (l.val <= r.val) {
p.next = l;
l = l.next;
} else {
p.next = r;
r = r.next;
}
p = p.next;
}
if (l != null) p.next = l;
if (r != null) p.next = r;
return res.next;
}
}