-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSortList.java
160 lines (127 loc) · 2.89 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.demo;
/**
* Sort a linked list in O(n log n) time using constant space complexity.
* @author kexun
*
*/
public class SortList {
public static void main(String[] args) {
ListNode head = new SortList().new ListNode(0);
ListNode a = new SortList().new ListNode(9);
ListNode b = new SortList().new ListNode(3);
ListNode c = new SortList().new ListNode(0);
ListNode d = new SortList().new ListNode(1);
head.next = a;
a.next = b;
b.next = c;
c.next = d;
SortList s = new SortList();
// ListNode ne = s.sortList(head);
// while (ne != null) {
// System.out.println(ne.val);
// ne = ne.next;
// }
s.sortList2(head);
}
public ListNode sortList(ListNode head) {
if (head == null) {
return null;
}
if (head.next == null) {
return head;
}
ListNode newHead = new ListNode(0);
newHead.next = new ListNode(head.val);
ListNode curr = head.next;
while (curr != null) {
ListNode newPre = newHead;
ListNode newCurr = newHead.next;
while (newCurr != null) {
if (newCurr.val > curr.val) {
newPre.next = new ListNode(curr.val);
newPre.next.next = newCurr;
break;
} else {
newPre = newPre.next;
newCurr = newCurr.next;
}
}
if (newCurr == null) {
newPre.next = new ListNode(curr.val);
}
curr = curr.next;
}
return newHead.next;
}
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
/**
* 归并排序
* @param head
* @return
*/
public ListNode sortList2(ListNode head) {
if (head == null) {
return null;
}
if (head.next == null) {
return head;
}
ListNode n = mergeSort(head, null);
while (n != null) {
System.out.println(n.val);
n = n.next;
}
return n.next;
}
public ListNode mergeSort(ListNode s, ListNode e) {
if (s.next == e || s.next.next == null) {
ListNode h = new ListNode(s.val);
h.next = new ListNode(s.next.val);
return h;
}
ListNode oneStep = s;
ListNode twoStep = s;
while (twoStep != e && twoStep.next != null) {
oneStep = oneStep.next;
twoStep = twoStep.next;
if (twoStep.next != null) {
twoStep = twoStep.next;
}
}
ListNode l = mergeSort(s, oneStep);
ListNode r = mergeSort(oneStep, e);
return merge(l, r);
}
public ListNode merge(ListNode l, ListNode r) {
ListNode head = new ListNode(0);
ListNode mList = head;
while (l.next != null && r.next != null) {
if (r.next.val < l.next.val) {
mList.next = new ListNode(r.next.val);
r = r.next;
} else {
mList.next = new ListNode(l.next.val);
l = l.next;
}
mList = mList.next;
}
while (l.next != null) {
mList.next = new ListNode(l.next.val);
l = l.next;
mList = mList.next;
}
while (r.next != null) {
mList.next = new ListNode(r.next.val);
r = r.next;
mList = mList.next;
}
return head;
}
}