-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy path148-sort-list.js
192 lines (171 loc) · 4.14 KB
/
148-sort-list.js
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
function sortList(head) {
quickSort(head, null);
return head;
}
function quickSort(head, tail) {
if (head == tail) {
return;
}
const slow = partition(head, tail);
quickSort(head, slow);
quickSort(slow.next, tail);
}
function swap(node1, node2) {
let tmp = node1.val;
node1.val = node2.val;
node2.val = tmp;
}
function partition(head, tail) {
let slow = head,
fast = head.next;
let p = head.val;
while (fast != tail) {
if (fast.val <= p) {
slow = slow.next;
swap(slow, fast);
}
fast = fast.next;
}
swap(head, slow);
return slow;
}
// another
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
function sortList(head) {
if(head == null || head.next == null) return head
let slow = head, fast = head, pre = null
while(fast && fast.next) {
pre = slow
slow = slow.next
fast = fast.next.next
}
pre.next = null
const left = sortList(head)
const right = sortList(slow)
return merge(left, right)
}
function merge(left, right) {
const dummy = new ListNode()
let cur = dummy
while(left && right) {
if (left.val <= right.val) {
cur.next = left
left = left.next
} else {
cur.next = right
right = right.next
}
cur = cur.next
}
if(left) {
cur.next = left
}
if(right) {
cur.next = right
}
return dummy.next
}
// another
function sortList(head) {
quickSort(head, null);
return head;
}
function quickSort( head, tail){
if (head == tail) {
return;
}
let slow = head, fast = head.next;
let p = head.val;
while (fast != tail){
if (fast.val <= p){
slow = slow.next;
swap(slow, fast);
}
fast = fast.next;
}
swap(head, slow);
quickSort(head, slow);
quickSort(slow.next, tail);
}
function swap( node1, node2){
let tmp = node1.val;
node1.val = node2.val;
node2.val = tmp;
}
// another
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
const sortList = function(head) {
let dummy = new ListNode(0);
dummy.next = head;
const list = [];
let done = (null === head);
// Keep partitioning our list into bigger sublists length. Starting with a size of 1 and doubling each time
for (let step = 1; !done; step *= 2) {
done = true;
let prev = dummy;
let remaining = prev.next;
do {
// Split off two sublists of size step
for (let i = 0; i < 2; ++i) {
list[i] = remaining;
let tail = null;
for (let j = 0; j < step && null != remaining; ++j, remaining = remaining.next) {
tail = remaining;
}
// Terminate our sublist
if (null != tail) {
tail.next = null;
}
}
// We're done if these are the first two sublists in this pass and they
// encompass the entire primary list
done &= (null == remaining);
// If we have two sublists, merge them into one
if (null != list[1]) {
while (null != list[0] || null != list[1]) {
let idx = (null == list[1] || null != list[0] && list[0].val <= list[1].val) ? 0 : 1;
prev.next = list[idx];
list[idx] = list[idx].next;
prev = prev.next;
}
// Terminate our new sublist
prev.next = null;
} else {
// Only a single sublist, no need to merge, just attach to the end
prev.next = list[0];
}
} while (null !== remaining);
}
return dummy.next;
}