-
Notifications
You must be signed in to change notification settings - Fork 0
/
22-23. 合并 K 个升序链表.ts
52 lines (47 loc) · 1.17 KB
/
22-23. 合并 K 个升序链表.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
export 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 {
if (!lists.length) return null;
if (lists.length === 1) return lists[0];
// 封装为优先级队列
class Queue {
private data: Array<ListNode | null> = [];
constructor(data: Array<ListNode | null>) {
this.data = data.filter(el => el);
this.sort();
}
sort() {
this.data.sort((a, b) => a!.val - b!.val);
}
pop() {
const node = this.data.shift();
if (!node) return null
if (node.next) {
this.data.push(node.next);
this.sort();
}
return node.val;
}
}
const q1 = new Queue(lists);
let start: ListNode | null = null;
let tail: ListNode | null = null;
let node = q1.pop();
while (node !== null) {
if (!start) {
start = new ListNode(node);
tail = start;
} else {
tail!.next = new ListNode(node);
tail = tail!.next;
}
node = q1.pop();
}
return start
};