Concept Flow - Merge K Sorted Lists Using Min Heap
Create min heap
↓
Insert first node of each list
↓
While heap not empty
↓
Extract min node
↓
Add min node to merged list
↓
If min node has next, insert next into heap
↓
Repeat until heap empty
↓
Return merged list head
Build a min heap with first nodes, repeatedly extract smallest, add to merged list, and insert next nodes until all lists merge.
Execution Sample
DSA Javascript
function mergeKLists(lists) {
const minHeap = new MinHeap();
for (const node of lists) if (node) minHeap.insert(node);
let dummy = new ListNode(0), current = dummy;
while (!minHeap.isEmpty()) {
let minNode = minHeap.extractMin();
current.next = minNode;
current = current.next;
if (minNode.next) minHeap.insert(minNode.next);
}
return dummy.next;
}
Merges k sorted linked lists by using a min heap to always pick the smallest node.
Execution Table
Step
Operation
Nodes in Heap (values)
Extracted Node
Merged List State
Heap After Operation
1
Insert first nodes of all lists
[1, 2, 3]
None
Empty
[1, 2, 3]
2
Extract min node
[1, 2, 3]
1
1 -> null
[2, 3]
3
Insert next of extracted node (1.next=4)
[2, 3]
None
1 -> null
[2, 3, 4]
4
Extract min node
[2, 3, 4]
2
1 -> 2 -> null
[3, 4]
5
Insert next of extracted node (2.next=5)
[3, 4]
None
1 -> 2 -> null
[3, 4, 5]
6
Extract min node
[3, 4, 5]
3
1 -> 2 -> 3 -> null
[4, 5]
7
Insert next of extracted node (3.next=6)
[4, 5]
None
1 -> 2 -> 3 -> null
[4, 5, 6]
8
Extract min node
[4, 5, 6]
4
1 -> 2 -> 3 -> 4 -> null
[5, 6]
9
Insert next of extracted node (4.next=null)
[5, 6]
None
1 -> 2 -> 3 -> 4 -> null
[5, 6]
10
Extract min node
[5, 6]
5
1 -> 2 -> 3 -> 4 -> 5 -> null
[6]
11
Insert next of extracted node (5.next=null)
[6]
None
1 -> 2 -> 3 -> 4 -> 5 -> null
[6]
12
Extract min node
[6]
6
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
[]
13
Heap empty, stop
[]
None
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
[]
💡 Heap is empty, all nodes merged into one sorted list.
Variable Tracker
Variable
Start
After Step 2
After Step 4
After Step 6
After Step 8
After Step 10
After Step 12
Final
minHeap
[]
[2,3]
[3,4]
[4,5]
[5,6]
[6]
[]
[]
mergedList
Empty
1 -> null
1 -> 2 -> null
1 -> 2 -> 3 -> null
1 -> 2 -> 3 -> 4 -> null
1 -> 2 -> 3 -> 4 -> 5 -> null
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
current
dummy
Node 1
Node 2
Node 3
Node 4
Node 5
Node 6
Node 6
Key Moments - 3 Insights
Why do we insert only the next node of the extracted node into the heap?
Because each list is sorted, after extracting the smallest node, only its next node can be the next smallest candidate. This keeps the heap size small and ensures correct order, as shown in steps 3, 5, 7, 9, and 11.
What happens if one of the input lists is empty?
If a list is empty, its first node is null and not inserted into the heap initially (step 1). The algorithm continues with the other lists without errors, merging only available nodes.
How does the heap help in merging k sorted lists efficiently?
The heap always keeps the smallest current nodes from each list at the top, so extracting min gives the next smallest node globally. This avoids scanning all lists repeatedly, as seen in the heap states in the execution table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the heap state after step 5?
A[3, 4, 5]
B[2, 3, 4]
C[4, 5, 6]
D[5, 6]
💡 Hint
Check the 'Heap After Operation' column for step 5 in the execution table.
At which step does the merged list first contain the node with value 3?
AStep 4
BStep 6
CStep 8
DStep 2
💡 Hint
Look at the 'Merged List State' column and find when '3' appears.
If the input lists had no next nodes (all single nodes), how would the heap size change after each extraction?
AHeap size increases after each extraction
BHeap size stays the same
CHeap size decreases by one each extraction
DHeap size becomes zero immediately
💡 Hint
Refer to the heap insertions after extraction steps in the execution table.
Concept Snapshot
Merge K Sorted Lists Using Min Heap:
- Insert first node of each list into min heap
- Extract min node from heap and add to merged list
- Insert next node of extracted node into heap if exists
- Repeat until heap empty
- Result is one merged sorted linked list
Full Transcript
This visualization shows how to merge k sorted linked lists using a min heap. First, the algorithm inserts the first node of each list into the min heap. Then, it repeatedly extracts the smallest node from the heap and adds it to the merged list. If the extracted node has a next node, that next node is inserted into the heap. This process continues until the heap is empty, meaning all nodes have been merged. The execution table tracks the heap contents, extracted nodes, and the merged list state at each step. The variable tracker shows how the heap and merged list pointers change over time. Key moments clarify why only next nodes are inserted and how empty lists are handled. The visual quiz tests understanding of heap states and merged list growth. This method efficiently merges k sorted lists by always choosing the smallest available node using the min heap.