Merge k sorted linked lists into one sorted linked list and return it.
Keep the current head of every list in a min-heap. Pop the smallest, append it, and push its successor.
1function mergeKLists(lists: (ListNode | null)[]): ListNode | null {2const heap = new PriorityQueue<ListNode>((a, b) => a.val - b.val);3for (const l of lists) if (l) heap.enqueue(l);4const dummy = new ListNode(0);5let tail = dummy;6while (!heap.isEmpty()) {7const node = heap.dequeue()!;8tail = tail.next = node;9if (node.next) heap.enqueue(node.next);10}11return dummy.next;12}
Push the head of each list.
Space: play/pause · ←/→: step