Merge k Sorted Lists
Merge k sorted linked lists into one sorted list in O(N log k) time using a priority queue (min-heap) of list heads.
01The problem
You are given an array of k linked-lists lists, each linked-list is sorted in ascending order.
Merge all the linked-lists into one sorted linked-list and return it.
The total number of nodes N across all lists can reach 10,000, and k can be up to 10,000.
| Input | Output | Why |
|---|---|---|
lists = [[1, 4, 5], [1, 3, 4], [2, 6]] | [1, 1, 2, 3, 4, 4, 5, 6] | All elements combined into single sorted list |
lists = [] | [] | Empty list array |
lists = [[]] | [] | Single empty list |
lists = [[5], [], [1]] | [1, 5] | Handles empty list entries safely |
Constraints: 0 to 10,000 lists, 0 to 500 nodes per list, total nodes N <= 10,000, node values between -10,000 and 10,000.
Loading the rest of the lesson…