Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix][javascript] merge-k-sorted-lists #1575

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions 多语言解法代码/solution_code.md
Original file line number Diff line number Diff line change
Expand Up @@ -39018,20 +39018,21 @@ var mergeKLists = function(lists) {
let dummy = new ListNode(-1);
let p = dummy;
// 优先级队列,最小堆
let pq = new PriorityQueue(
lists.length, (a, b) => (a.val - b.val));
let pq = new PriorityQueue({
compare: (a, b) => (a.val - b.val)
});
// 将 k 个链表的头结点加入最小堆
for (let head of lists) {
if (head != null)
pq.add(head);
pq.enqueue(head);
}

while (!pq.isEmpty()) {
// 获取最小节点,接到结果链表中
let node = pq.poll();
let node = pq.dequeue();
p.next = node;
if (node.next != null) {
pq.add(node.next);
pq.enqueue(node.next);
}
// p 指针不断前进
p = p.next;
Expand Down