Quick revision
The short answer
With a min-heap, we put the head of every non-empty list into a PriorityQueue ordered by value, then repeatedly poll the smallest node, append it to the result and offer its next node; that is O(N log k) time and O(k) heap space. With divide and conquer, we merge the lists in pairs, then pairs of the results, for log k rounds that each touch all N nodes, which is also O(N log k) with no heap. Merging the lists one by one into a growing result is O(N k), billions of steps too slow for k = 10,000. For this size I would choose the heap, skip null lists when seeding it, and relink the existing nodes instead of allocating 10 million new ones.
What to remember
In this answer, we saw that merging k sorted lists one by one costs O(N k), about 5 x 10^10 steps for our numbers, which is too slow. A min-heap of the k current heads gives O(N log k) time with O(k) space and produces output incrementally, and divide and conquer merging in pairs gives the same complexity with no heap. For this size both are well under a second or two. I would choose the heap, skip null lists when seeding it, relink existing nodes instead of allocating, and switch to an external k-way merge with buffered readers when the lists are files.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
With a min-heap, we put the head of every non-empty list into a PriorityQueue ordered by value, then repeatedly poll the smallest node, append it to the result and offer its next node; that is O(N log k) time and O(k) heap space. With divide and conquer, we merge the lists in pairs, then pairs of the results, for log k rounds that each touch all N nodes, which is also O(N log k) with no heap. Merging the lists one by one into a growing result is O(N k), billions of steps too slow for k = 10,000. For this size I would choose the heap, skip null lists when seeding it, and relink the existing nodes instead of allocating 10 million new ones.
Why this question is asked
Merging many sorted sequences is a real production task: combining sorted log files from many servers, merging results from database shards, or the final phase of an external sort. Interviewers ask it with concrete numbers so we have to reason about which complexity is acceptable and about memory, not just recite an algorithm. In this answer, we will recap merging two lists, see why the naive approach fails at this size, build the heap solution, build the divide and conquer solution, compare them with real numbers, and choose. We will finish with lists that live in files on disk.
1A Quick Recap of Merging Two Sorted Lists
Merging two sorted lists is the building block. We keep a pointer into each list, compare the two current nodes, append the smaller one to the result and advance its pointer, until one list runs out, and then we attach the rest of the other. Each step places one node, so merging lists of sizes p and q takes p + q steps.
A dummy node, a placeholder at the front of the result, avoids special cases for the first node: we append after it and return dummy.next at the end.
Notice that we relink the existing nodes rather than creating new ones. With 10 million nodes, allocating copies would create 10 million extra objects for the garbage collector, a few hundred megabytes of short-lived garbage.
2Why Merging One by One Is Too Slow
The obvious approach merges list 1 with list 2, then merges the result with list 3, and so on. The problem is that the result keeps growing and gets walked again in every merge. If the N nodes are spread evenly, the result after merging i lists holds about i N / k nodes, so the total work is about N / k x (1 + 2 + ... + k), roughly N k / 2. With N = 10 million and k = 10,000, that is about 5 x 10^10 node steps, tens of seconds to minutes of work, compared with about 1.3 x 10^8 for the approaches below.
3Approach 1: A Min-Heap of List Heads
At any moment, the next node of the merged result is the smallest of the k current heads. A min-heap, here Java's PriorityQueue, gives us the smallest of k items in O(log k) time.
- Seed the heapoffer each non-null head: at most k nodes
- Heap empty?YesReturn dummy.nextNoPoll the smallest node
- Append it to the resultrelink:
tail.next= node - Has a next node?YesOffer node.next to the heapNoNothing to add
- RepeatN polls in total, each
O(log k)
5 steps in order. Play them, or step through them one at a time.
Each of the N nodes is offered once and polled once, and each heap operation costs O(log k) with at most k nodes in the heap. So the time is O(N log k) and the extra space is O(k). Skipping null heads matters, because PriorityQueue.offer(null) throws NullPointerException, and in real data some of the 10,000 lists may well be empty. The comparator uses comparingInt, which avoids the overflow that a subtraction comparator would have.
4Approach 2: Divide and Conquer
Instead of merging everything into one growing result, we merge in rounds, like the merge phase of merge sort: list 0 with list 1, list 2 with list 3, and so on, then merge the results in pairs again.
Each round touches every node once, and the number of lists halves each round, so there are log2 k rounds, about 14 for k = 10,000. The total is O(N log k), the same as the heap. There is no heap and no comparator object, the inner loop is a plain comparison of two ints, and the recursion-free version above uses O(1) extra space beyond the array of heads. Each node is relinked about 14 times, while the heap version relinks each node once but does more work per node.
5Choosing Between Them
With N = 10 million and log2 k about 13.3, both approaches do on the order of 1.3 x 10^8 basic steps, a fraction of a second to a second or two in Java. In practice the divide and conquer version is often slightly faster for in-memory lists, because each step is a simple comparison and the active part of the work is two lists at a time. The heap version is more flexible: it produces output incrementally, one node at a time, so we can stream the result to a consumer or stop after the first million nodes, and it works when the lists are not all available at once.
For this question I would choose the heap: it is easy to reason about, streams naturally, and generalizes to files and network sources, as the follow-up shows. I would mention divide and conquer as the alternative when the lists are all in memory and we want the last bit of speed, and I would measure both with JMH before claiming one is faster. Whatever we choose, linked nodes are scattered in memory, so a real system with 10 million values might prefer sorted arrays, which are much friendlier to the CPU cache.
6Follow-up questions
How would this change if the lists are files on disk?
The algorithm becomes an external k-way merge. I would open a buffered reader for each file and keep a heap of small entries holding the current value and the index of the file it came from. Polling gives the next value to write to the output, and I read the next value from that same file. The heap stays O(k), and memory is dominated by the read buffers, so with 10,000 files I would size the buffers carefully or merge in several passes, for example 100 groups of 100 files, then the 100 results, because the operating system also limits how many files a process can keep open.
Why not collect all values into one array and sort it?
That is O(N log N) time and needs all 10 million values in memory as a new array, about 40 MB for ints, plus rebuilding the list. It ignores the fact that the inputs are already sorted. log N is about 23 here, against log k of about 13, so it does nearly twice the comparisons and much more memory traffic. It is a fine quick answer in a script, but not the efficient one.
7Conclusion
In this answer, we saw that merging k sorted lists one by one costs O(N k), about 5 x 10^10 steps for our numbers, which is too slow. A min-heap of the k current heads gives O(N log k) time with O(k) space and produces output incrementally, and divide and conquer merging in pairs gives the same complexity with no heap. For this size both are well under a second or two. I would choose the heap, skip null lists when seeding it, relink existing nodes instead of allocating, and switch to an external k-way merge with buffered readers when the lists are files.
Open with
I would give two approaches. The first is a min-heap. I put the head of every non-empty list into a PriorityQueue ordered by value, skipping nulls because a priority queue cannot hold null and some of the 10,000 lists may be empty.
Then, if they want more
Then I repeatedly poll the smallest node, append it to the result by relinking it, and offer its next node if there is one. Each of the 10 million nodes goes in and out of a heap of at most k entries once, so that is O(N log k), about 130 million basic steps, with O(k) extra space.
The second approach is divide and conquer: merge the lists in pairs, then the results in pairs, for about 14 rounds. Each round touches every node once, so it is also O(N log k), without a heap.
What I would not do is merge the lists one after another into a growing result, because the result is rescanned every time, which is O(N k), about 50 billion steps here. Between the two, I would choose the heap.
It is simple, it produces the merged output incrementally so I can stream it, and it extends directly to sources like files or network streams. Divide and conquer may be a bit faster when everything is in memory, and I would measure before deciding on speed alone.
In both I reuse the existing nodes instead of allocating new ones, which avoids 10 million extra objects for the garbage collector.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Merge k sorted linked lists into one sorted list in Java, where k can be 10,000 and the...": https://scrivio-interview-library.netlify.app/java/coding-problems/b228881c95/