Home
DSA Patterns
Heap / Priority Queue
Merge K Sorted Lists
Merge K Sorted Lists (LeetCode 23)
Merge K Sorted Lists (LeetCode 23): merge k sorted linked lists into one sorted linked list.
Problem Understanding
lists holds k linked lists, each already sorted in ascending order.
Combine them into a single linked list that is also sorted in ascending order, and return its head.
Example:
- Input:
lists = [[1,4,5],[1,3,4],[2,6]] - Output:
[1,1,2,3,4,4,5,6]
Algorithm Strategy
Min-Heap Approach (O(N log k))
Instead of comparing the heads of all k lists at every step (which takes O(k)), we can use a Min-Heap.
- Initialize: Push the head node of every non-empty list into a Min-Heap.
- Iterate: While heap is not empty:
- Pop the smallest node (root) from heap.
- Add this node to our result list.
- If the popped node has a
nextnode, push thatnextnode into the heap.
- Result: The result list is sorted.
Interactive Visualization
k = 3 · AT MOST ONE NODE PER LIST IN THE HEAP
PUSH HEAD
lists
L1
→
→
→ NULL
L2
→
→
→ NULL
L3
→
→ NULL
result
not started
Heap
1/3
Linked
0/8
Push the head of list 1: 1
Next
Push the head of list 2: 1
Lists
The k lists as chains, the min-heap holding the front node of each, and the result growing from a dummy node. Each pop links the smallest node into the result and pushes the node after it.
Dry Run
Lists: L1: [1,4,5], L2: [1,3,4], L3: [2,6]. A node is written value·list.
| Step | Pop | Push next | Heap after (root first) | Result |
|---|---|---|---|---|
| Init | - | heads 1·L1, 1·L2, 2·L3 | [1·L1, 1·L2, 2·L3] |
dummy |
| 1 | 1·L1 | 4·L1 | [1·L2, 2·L3, 4·L1] |
1 |
| 2 | 1·L2 | 3·L2 | [2·L3, 4·L1, 3·L2] |
1 → 1 |
| 3 | 2·L3 | 6·L3 | [3·L2, 4·L1, 6·L3] |
1 → 1 → 2 |
| 4 | 3·L2 | 4·L2 | [4·L1, 6·L3, 4·L2] |
… → 2 → 3 |
| 5 | 4·L1 | 5·L1 | [4·L2, 6·L3, 5·L1] |
… → 3 → 4 |
| 6 | 4·L2 | - (L2 ends) | [5·L1, 6·L3] |
… → 4 → 4 |
| 7 | 5·L1 | - (L1 ends) | [6·L3] |
… → 4 → 5 |
| 8 | 6·L3 | - (L3 ends) | [] |
1 → 1 → 2 → 3 → 4 → 4 → 5 → 6 |
Edge Cases
- No lists (
[]): the heap starts empty anddummy.nextis null. - Empty lists inside (
[[], [1]]): only non-null heads are pushed, a null would break the comparison. - One list: the heap holds one node at a time and the list comes back unchanged.
- Equal values: any order among equal nodes gives the same merged values. Python adds the list index
ito each tuple so a tie never falls through to comparing two nodes.
Solution
A min-heap of list heads in every language. JavaScript, TypeScript and C carry a small heap of their own.
Merge K Sorted Lists Code
1class Heap {2 constructor(before) {3 this.items = [];4 this.before = before;5 }67 size() {8 return this.items.length;9 }1011 peek() {12 return this.items[0];13 }1415 push(item) {16 const a = this.items;17 a.push(item);18 let i = a.length - 1;19 while (i > 0 && this.before(a[i], a[(i - 1) >> 1])) {20 const parent = (i - 1) >> 1;21 [a[i], a[parent]] = [a[parent], a[i]];22 i = parent;23 }24 }2526 pop() {27 const a = this.items;28 const top = a[0];29 const last = a.pop();30 if (a.length > 0) {31 a[0] = last;32 let i = 0;33 while (true) {34 const left = 2 * i + 1, right = 2 * i + 2;35 let first = i;36 if (left < a.length && this.before(a[left], a[first])) first = left;37 if (right < a.length && this.before(a[right], a[first])) first = right;38 if (first === i) break;39 [a[i], a[first]] = [a[first], a[i]];40 i = first;41 }42 }43 return top;44 }45}4647var mergeKLists = function(lists) {48 const heap = new Heap((a, b) => a.val < b.val);49 for (const node of lists) {50 if (node) heap.push(node);51 }52 const dummy = new ListNode(0);53 let tail = dummy;54 while (heap.size() > 0) {55 const node = heap.pop();56 tail.next = node;57 tail = node;58 if (node.next) heap.push(node.next);59 }60 return dummy.next;61};
Merge K Sorted Lists in Python, Java, C++, TypeScript and C
Merge K Sorted Lists in Python
import heapq
class Solution:
def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
heap = []
for i, node in enumerate(lists):
if node:
heapq.heappush(heap, (node.val, i, node))
dummy = ListNode(0)
tail = dummy
while heap:
val, i, node = heapq.heappop(heap)
tail.next = node
tail = node
if node.next:
heapq.heappush(heap, (node.next.val, i, node.next))
return dummy.nextMerge K Sorted Lists in Java
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
PriorityQueue<ListNode> heap = new PriorityQueue<>((a, b) -> a.val - b.val);
for (ListNode node : lists) {
if (node != null) heap.add(node);
}
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
while (!heap.isEmpty()) {
ListNode node = heap.poll();
tail.next = node;
tail = node;
if (node.next != null) heap.add(node.next);
}
return dummy.next;
}
}Merge K Sorted Lists in C++
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
auto later = [](ListNode* a, ListNode* b) { return a->val > b->val; };
priority_queue<ListNode*, vector<ListNode*>, decltype(later)> heap(later);
for (ListNode* node : lists) {
if (node) heap.push(node);
}
ListNode dummy(0);
ListNode* tail = &dummy;
while (!heap.empty()) {
ListNode* node = heap.top(); heap.pop();
tail->next = node;
tail = node;
if (node->next) heap.push(node->next);
}
return dummy.next;
}
};Merge K Sorted Lists in TypeScript
class Heap<T> {
items: T[] = [];
constructor(private before: (a: T, b: T) => boolean) {}
size(): number {
return this.items.length;
}
peek(): T {
return this.items[0];
}
push(item: T): void {
const a = this.items;
a.push(item);
let i = a.length - 1;
while (i > 0 && this.before(a[i], a[(i - 1) >> 1])) {
const parent = (i - 1) >> 1;
[a[i], a[parent]] = [a[parent], a[i]];
i = parent;
}
}
pop(): T {
const a = this.items;
const top = a[0];
const last = a.pop()!;
if (a.length > 0) {
a[0] = last;
let i = 0;
while (true) {
const left = 2 * i + 1, right = 2 * i + 2;
let first = i;
if (left < a.length && this.before(a[left], a[first])) first = left;
if (right < a.length && this.before(a[right], a[first])) first = right;
if (first === i) break;
[a[i], a[first]] = [a[first], a[i]];
i = first;
}
}
return top;
}
}
function mergeKLists(lists: Array<ListNode | null>): ListNode | null {
const heap = new Heap<ListNode>((a, b) => a.val < b.val);
for (const node of lists) {
if (node) heap.push(node);
}
const dummy = new ListNode(0);
let tail = dummy;
while (heap.size() > 0) {
const node = heap.pop();
tail.next = node;
tail = node;
if (node.next) heap.push(node.next);
}
return dummy.next;
}Merge K Sorted Lists in C
static void push(struct ListNode** heap, int* size, struct ListNode* item) {
int i = (*size)++;
heap[i] = item;
while (i > 0 && heap[(i - 1) / 2]->val > heap[i]->val) {
int parent = (i - 1) / 2;
struct ListNode* t = heap[i]; heap[i] = heap[parent]; heap[parent] = t;
i = parent;
}
}
static struct ListNode* pop(struct ListNode** heap, int* size) {
struct ListNode* top = heap[0];
heap[0] = heap[--(*size)];
int i = 0;
while (1) {
int left = 2 * i + 1, right = 2 * i + 2, smallest = i;
if (left < *size && heap[left]->val < heap[smallest]->val) smallest = left;
if (right < *size && heap[right]->val < heap[smallest]->val) smallest = right;
if (smallest == i) break;
struct ListNode* t = heap[i]; heap[i] = heap[smallest]; heap[smallest] = t;
i = smallest;
}
return top;
}
struct ListNode* mergeKLists(struct ListNode** lists, int listsSize) {
struct ListNode** heap = malloc((listsSize + 1) * sizeof(struct ListNode*));
int size = 0;
for (int i = 0; i < listsSize; i++) {
struct ListNode* node = lists[i];
if (node) push(heap, &size, node);
}
struct ListNode dummy = {0, NULL};
struct ListNode* tail = &dummy;
while (size > 0) {
struct ListNode* node = pop(heap, &size);
tail->next = node;
tail = node;
if (node->next) push(heap, &size, node->next);
}
free(heap);
return dummy.next;
}More practice: DSA interview questions with answers · patterns cheat sheet
