forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortList.java
97 lines (89 loc) · 2.67 KB
/
SortList.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
/**
* 这题采用merge sort,可自顶向下或自底向上
* 区别是自顶向下是递归的,需要额外的空间
* 而自底向上不用
* 时间复杂度都是O(nlgn)
*/
public class SortList {
/**
* 自顶向下
*/
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;
}
prev.next = null;
ListNode l1 = sortList(head);
ListNode l2 = sortList(slow);
return merge(l1, l2);
}
ListNode merge(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0), cur = dummy;
for ( ; l1 != null && l2 != null; ) {
if (l1.val < l2.val) {
cur.next = l1;
l1 = l1.next;
} else {
cur.next = l2;
l2 = l2.next;
}
cur = cur.next;
}
cur.next = l1 != null ? l1 : l2;
return dummy.next;
}
/**
* 自底向上
*/
public ListNode sortList2(ListNode head) {
int size = 0;
for (ListNode node = head; node != null; node = node.next, size++);
ListNode dummy = new ListNode(0);
ListNode left, right, cur, tail;
for (int step = 1; step <= size; step *= 2) {
for (cur = head, tail = dummy; cur != null; ) {
left = cur;
right = split(left, step);
cur = split(right, step);
tail = merge(left, right, tail);
}
head = dummy.next;
}
return dummy.next;
}
/**
* 将l1和l2合并后挂在tail下,返回新的tail
*/
ListNode merge(ListNode l1, ListNode l2, ListNode tail) {
for ( ; l1 != null && l2 != null; ) {
if (l1.val < l2.val) {
tail.next = l1;
l1 = l1.next;
} else {
tail.next = l2;
l2 = l2.next;
}
tail = tail.next;
}
tail.next = l1 != null ? l1 : l2;
for ( ; tail.next != null; tail = tail.next);
return tail;
}
/**
* 从start开始取step个节点,返回下一个节点
*/
private ListNode split(ListNode start, int step) {
for (step--; start != null && step > 0; step--, start = start.next);
if (start != null && start.next != null) {
ListNode next = start.next;
start.next = null;
return next;
}
return null;
}
}