-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSortList.java
More file actions
96 lines (90 loc) · 2.03 KB
/
SortList.java
File metadata and controls
96 lines (90 loc) · 2.03 KB
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
/**
*
*/
package cc.dectinc.leetcode;
import cc.dectinc.api.structs.ListNode;
/**
* @author Dectinc
* @version Apr 16, 2015 9:46:01 PM
*
*/
public class SortList {
public ListNode sortList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode fast = head.next;
ListNode slow = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
fast = slow.next;
slow.next = null;
head = sortList(head);
fast = sortList(fast);
return merge(head, fast);
}
private ListNode merge(ListNode head1, ListNode head2) {
ListNode dummy = new ListNode(0);
ListNode p = dummy;
while (head1 != null && head2 != null) {
if (head1.val > head2.val) {
p.next = head2;
head2 = head2.next;
} else {
p.next = head1;
head1 = head1.next;
}
p = p.next;
}
if (head1 != null) {
p.next = head1;
}
if (head2 != null) {
p.next = head2;
}
return dummy.next;
}
@SuppressWarnings("unused")
private void quickSortList(ListNode head, ListNode tail) {
if (head == null || head.next == null) {
return;
}
int val = head.val;
ListNode r = head.next;
ListNode p = new ListNode(0), dummy1 = p;
ListNode q = new ListNode(0), dummy2 = q;
while (r != tail) {
if (r.val < val) {
p.next = r;
p = r;
} else {
q.next = r;
q = r;
}
r = r.next;
}
p.next = head;
head.next = dummy2.next;
q.next = tail;
head = dummy1.next;
quickSortList(dummy1.next, p);
quickSortList(dummy2.next, tail);
}
@SuppressWarnings("unused")
private void swapNodes(ListNode p, ListNode q) {
ListNode r = p;
p = q;
q = r;
}
public static void main(String[] args) {
SortList sol = new SortList();
ListNode head = ListNode.constructList(new Integer[] { 5, 1, 4, 0, 3, 2 });
System.out.println(sol.sortList(head));
// ListNode head1 = ListNode.constructList(new Integer[] { 1, 3, 5 });
// ListNode head2 = ListNode.constructList(new Integer[] { 1, 2, 4, 6
// });
// System.out.println(sol.merge(head1, head2));
}
}