forked from webVueBlog/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
合并.js
87 lines (70 loc) · 1.31 KB
/
合并.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
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode[]} lists
* @return {ListNode}
23. 合并K个升序链表
let t = p.next.next;
const p = {
val: 1,
next: {
val: 4,
next: {}
}
}
[1,4,5]
i
[1,3,4]
j
[2,6]
k
最小堆 [空着, 1, 1, 2]
堆顶
[1, 1, 2]
// 堆,分治
queue = [[2,6,1,4,5,1,3,4]]
[1,4,5]
i
[1,3,4]
j
1->1->3->4->4->5...
*/
var mergeKLists = function(lists) {
if(!lists.length) return null;
const queue = [...lists]; // 队列
while(queue.length >= 2) { // 至少两个队列
// 合并两个链表
const l1 = queue.shift();
const l2 = queue.shift();
const l3 = merge(l1, l2);
queue.push(l3);
}
return queue[0];
};
function merge(l1, l2) {
const head = new ListNode();
// null ->
let p = head;
while (l1 && l2) {
if(l1.val < l2.val) {
p.next = l1;
l1 = l1.next;
} else {
p.next = l2;
l2 = l2.next;
}
p = p.next;
}
if(l1) {
p.next = l1;
}
if(l2) {
p.next = l2;
}
return head.next;
}