Merge K Sorted Lists
Keep the current heads in a min-heap so the global smallest is always one pop away.
Heads: 1 in a, 2 in b, 3 in c. The smallest is 1 from a, so it is unhooked from its list and linked after nothing: it becomes the merged head.
What you will see
k list heads sit in a heap; the minimum pops out and its list advances.
Cost
| Best | O(N log k) |
|---|---|
| Average | O(N log k) |
| Worst | O(N log k) |
| Space | O(k) |
N total nodes across k lists.
How you work with it here
play it through, step one change at a time, scrub to any step, run it on your own input, predict what happens next, try operations in any order.
Screen readers: Each node announces its value and what it points to; each step announces the pointer change ("node 3 now points to node 5").
Reduced motion: Pointer arrows redraw in place with a brief emphasis instead of animating the detach and reattach.
Before this
Related
Taught by the same lesson
Merging Sorted Lists covers these too, in the same run.