Skip to content

Commit 27fac22

Browse files
committed
feat: 23. Merge k Sorted Lists
1 parent a69c944 commit 27fac22

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

merge-k-sorted-lists/gwbaik9717.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// k: len(lists), n: Total number of nodes
2+
// Time complexity: O(nlogk)
3+
// Space complexity: O(n)
4+
5+
class MinHeap {
6+
constructor() {
7+
this.heap = [null];
8+
}
9+
10+
isEmpty() {
11+
return this.heap.length === 1;
12+
}
13+
14+
push(listNode) {
15+
this.heap.push(listNode);
16+
17+
let current = this.heap.length - 1;
18+
let parent = Math.floor(current / 2);
19+
20+
while (parent !== 0) {
21+
if (this.heap[parent].val > listNode.val) {
22+
[this.heap[parent], this.heap[current]] = [
23+
this.heap[current],
24+
this.heap[parent],
25+
];
26+
27+
current = parent;
28+
parent = Math.floor(current / 2);
29+
continue;
30+
}
31+
32+
break;
33+
}
34+
}
35+
36+
pop() {
37+
if (this.heap.length === 1) {
38+
return;
39+
}
40+
41+
if (this.heap.length === 2) {
42+
return this.heap.pop();
43+
}
44+
45+
const rv = this.heap[1];
46+
47+
this.heap[1] = this.heap.pop();
48+
49+
let current = 1;
50+
let left = current * 2;
51+
let right = left + 1;
52+
53+
while (
54+
(this.heap[left] && this.heap[current].val > this.heap[left].val) ||
55+
(this.heap[right] && this.heap[current].val > this.heap[right].val)
56+
) {
57+
if (this.heap[left] && this.heap[right]) {
58+
if (this.heap[left].val < this.heap[right].val) {
59+
[this.heap[left], this.heap[current]] = [
60+
this.heap[current],
61+
this.heap[left],
62+
];
63+
current = left;
64+
} else {
65+
[this.heap[right], this.heap[current]] = [
66+
this.heap[current],
67+
this.heap[right],
68+
];
69+
current = right;
70+
}
71+
72+
left = current * 2;
73+
right = left + 1;
74+
continue;
75+
}
76+
77+
[this.heap[left], this.heap[current]] = [
78+
this.heap[current],
79+
this.heap[left],
80+
];
81+
current = left;
82+
left = current * 2;
83+
right = left + 1;
84+
}
85+
86+
return rv;
87+
}
88+
}
89+
90+
/**
91+
* Definition for singly-linked list.
92+
* function ListNode(val, next) {
93+
* this.val = (val===undefined ? 0 : val)
94+
* this.next = (next===undefined ? null : next)
95+
* }
96+
*/
97+
/**
98+
* @param {ListNode[]} lists
99+
* @return {ListNode}
100+
*/
101+
var mergeKLists = function (lists) {
102+
const minHeap = new MinHeap();
103+
104+
for (const list of lists) {
105+
if (list) {
106+
minHeap.push(list);
107+
}
108+
}
109+
110+
let answer = null;
111+
let next = null;
112+
113+
while (!minHeap.isEmpty()) {
114+
const current = minHeap.pop();
115+
116+
if (!answer) {
117+
answer = new ListNode();
118+
next = answer;
119+
}
120+
121+
next.val = current.val;
122+
123+
if (current.next) {
124+
minHeap.push(current.next);
125+
}
126+
127+
if (!minHeap.isEmpty()) {
128+
next.next = new ListNode();
129+
next = next.next;
130+
}
131+
}
132+
133+
return answer;
134+
};

0 commit comments

Comments
 (0)