-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path0148-sort-list.js
49 lines (45 loc) · 1.01 KB
/
0148-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
/**
* 148. Sort List
* https://leetcode.com/problems/sort-list/
* Difficulty: Medium
*
* Given the head of a linked list, return the list after sorting it in ascending order.
*/
/**
* 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}
*/
var sortList = function(head) {
if (!head || !head.next) return head;
let fast = head;
let slow = head;
while (fast.next && fast.next.next) {
fast = fast.next.next;
slow = slow.next;
}
const middle = slow.next;
slow.next = null;
return mergeList(sortList(head), sortList(middle));
};
function mergeList(a, b) {
const ordered = new ListNode(-1);
let list = ordered;
while (a && b) {
list.next = a.val < b.val ? a : b;
list = list.next;
if (a.val < b.val) {
a = a.next;
} else {
b = b.next;
}
}
list.next = a ?? b;
return ordered.next;
}