We have k linked lists, each already sorted, and we need to merge them into a single sorted linked list. This generalizes the "merge two sorted lists" problem from two inputs to k inputs.
Because each list is sorted, the next element of the merged result is always the smallest among the current heads of the k lists. The work, then, is finding that minimum repeatedly. Scanning all k heads each time costs O(k) per element. A min-heap returns the minimum in O(log k) instead.
A second strategy is divide and conquer. Instead of merging all k lists at once, pair them up, merge each pair, then pair up the results and repeat. This mirrors the merge phase of merge sort and reaches the same time complexity as the heap.
0 <= k <= 10^4 → k can be large, and k can be 0, so an empty input array must return null without crashing.The sum of lists[i].length will not exceed 10^4 → The total number of nodes N is at most 10,000, which is what drives the time complexity.-10^4 <= lists[i][j] <= 10^4 → Node values fit in a 32-bit signed integer, so comparisons never overflow.Ignore the linked list structure entirely. Walk through every node in every list, collect all the values into a single array, sort that array, then build a new linked list from the sorted values.
This discards the fact that the input lists are already sorted, but it produces a correct answer and serves as a baseline to improve on.
Sorting throws away the sorted structure we were handed and pays O(N log N) to rebuild it. The next approach exploits that structure to pick the smallest element in O(log k) per step.
Since each list is sorted, the next element in the merged result is the smallest among the current heads of the k lists. A min-heap holds those heads and returns the smallest in O(log k) per operation, compared to O(k) for scanning all heads.
Push all k initial heads into the heap. Then repeatedly pop the smallest node, append it to the result, and push that node's next pointer back into the heap if it exists. The process ends when the heap is empty.
The heap holds at most one node per list, so it never exceeds k entries. Each list is sorted, so a list's smallest unprocessed node is its current head, and the global minimum across all lists is therefore one of the k heads in the heap. Popping that minimum and pushing only its successor preserves this invariant, so the values come out in non-decreasing order. The k bound on heap size is why each operation costs O(log k) rather than O(log N).
The next approach reaches the same O(N log k) time without a heap, using only a function that merges two sorted lists.
Borrow the structure of merge sort. Pair up the k lists, merge each pair with the standard "merge two sorted lists" routine, and repeat until one list remains.
The first round merges k lists into k/2, the second into k/4, and so on, so after log(k) rounds a single list remains. Within each round, every node is touched once because each node belongs to exactly one of the pairs being merged. That makes a round O(N), and log(k) rounds give O(N log k), matching the heap. The only building block needed is the two-list merge.
Why halving instead of merging one list at a time into an accumulator? Folding each list into a growing accumulator re-scans the accumulator on every step, which gives O(N k) total. Pairing keeps the merged lengths balanced so no node is scanned more than log(k) times.