Merge k Sorted Lists
LeetCode 23 • Hard • Linked Lists / Heaps
Input: lists = [[1,4,5],[1,3,4],[2,6]] → Output: [1,1,2,3,4,4,5,6]. Min-heap of k heads. Pop min, append, push next. O(n log k).
TimeO(n log k)
SpaceO(k)
Heap: []
Result: []
Min-heap
Result
Current head
heap[]
result[]
Ready
Press Play. Min-heap stores (val, listIdx). Pop min, append to result, push next from that list.