-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23.合并k个升序链表.js
101 lines (96 loc) · 2.09 KB
/
23.合并k个升序链表.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
/*
* @lc app=leetcode.cn id=23 lang=javascript
*
* [23] 合并K个升序链表
*/
// @lc code=start
/**
* 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}
*/
function ListNode(val, next) {
this.val = val === undefined ? 0 : val
this.next = next === undefined ? null : next
}
class Heap {
constructor(comparator) {
this._heap = [null]
this._comparator = comparator
}
insert(element) {
this._heap.push(element)
this._swim(this._heap.length - 1)
}
poll() {
let top = this._heap[1]
// to avoid holes in the array, so we move swap the the first(greateset) element and last
this._swap(1, this._heap.length - 1)
// remove the last
this._heap.pop()
// heapify
this._sink(1)
return top
}
isEmpty() {
return this._heap.length === 1
}
_swim(k) {
if (k === 1) return
while (k > 1 && this._compare(Math.floor(k / 2), k)) {
let i = Math.floor(k / 2)
this._swap(i, k)
k = i
}
}
_sink(k) {
while (2 * k < this._heap.length - 1) {
let j = 2 * k
if (j < this._heap.length && this._compare(j, j + 1)) {
j++
}
// can not move down any more
if (!this._compare(k, j)) break
this._swap(k, j)
k = j
}
}
_compare(a, b) {
return this._comparator(this._heap[a], this._heap[b])
}
_swap(a, b) {
let temp = this._heap[a]
this._heap[a] = this._heap[b]
this._heap[b] = temp
}
}
var mergeKLists = function(lists) {
if (lists.length === 0) return null
let dummy = new ListNode(-1)
let p = dummy
const pq = new Heap((a, b) => a.val > b.val)
for (let i = 0; i < lists.length; i++) {
const head = lists[i]
if (head) {
pq.insert(head)
}
}
while (!pq.isEmpty()) {
const node = pq.poll()
p.next = node
if (node.next) {
pq.insert(node.next)
}
p = p.next
}
return dummy.next
}
// @lc code=end
// no test
module.exports = { mergeKLists }