-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.ts
68 lines (61 loc) · 1.58 KB
/
solution.ts
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
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function mergeKLists (lists: Array<ListNode | null>): ListNode | null {
lists = lists.filter(item => item);
if (!lists.length) {
return null;
}
const dummyHead = new ListNode(0);
let last = dummyHead;
heapify(lists);
while (lists.length > 1) {
const node = lists[0];
const next = node.next;
node.next = null;
last.next = node;
last = node;
if (next) {
lists[0] = next;
sideDown(lists, 0);
} else {
lists[0] = lists.pop();
sideDown(lists, 0);
}
}
last.next = lists[0];
return dummyHead.next;
}
function swap<T> (arr:T[], i:number, j:number) {
const tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
function sideDown (heap:ListNode[], index:number) {
while (2 * index + 1 < heap.length) {
let child = 2 * index + 1;
if (child + 1 < heap.length && heap[child].val > heap[child + 1].val) {
child++;
}
if (heap[child].val < heap[index].val) {
swap(heap, index, child);
index = child;
} else {
break;
}
}
}
function heapify (heap:ListNode[]) {
let index = (heap.length - 2) >> 1;
while (index > -1) {
sideDown(heap, index--);
}
}