23. Merge k Sorted Lists

202407261511
tags: #linked-list

var mergeKLists = function(lists) {
	const queue = new MinPriorityQueue({ priority: x => x.val });
	for (const node of lists) {
		if (node) {
			queue.enqueue(node);
		}
	}
	const res = new ListNode();
	let cur = res;
	while (!queue.isEmpty()) {
		const node = queue.dequeue().element;
		cur.next = node;
		cur = cur.next;
		if (node.next) {
			queue.enqueue(node.next);
			node.next = null;
		}
	}
	return res.next;
};

Reference

https://leetcode.com/problems/merge-k-sorted-lists