← Create New Paste

Merge K Sorted Lists: A Classic Heap Interview Problem Explained

7 views
"Merge k sorted linked lists into one sorted list" is a favorite interview question because it starts simple and opens into a discussion of heaps, divide and conquer, and complexity trade-offs. Here is how to work through it clearly. Clarify the problem You receive an array of k linked lists, each sorted in ascending order. Return a single sorted linked list containing all nodes. Ask about edge cases: can the array be empty, can some lists be empty, and how large are k and the total number of nodes, N? These numbers shape which solution is best. The brute-force baseline Collect every value into an array, sort it, and build a new list. This runs in O(N log N) time and O(N) extra space. It is a fine starting point to mention, but it ignores the fact that each list is already sorted. Approach 1: A min-heap Since each list is sorted, the next smallest node overall must be the head of one of the k lists. A min-heap keeps track of those heads efficiently. 1. Push the head of each non-empty list into the heap. 2. Pop the smallest node, attach it to the result, and push its next node if one exists. 3. Repeat until the heap is empty. Each of the N nodes is pushed and popped once, and the heap holds at most k items, so the time is O(N log k) and the extra space is O(k). Python detail Python's heapq compares tuples element by element, and ListNode objects are not comparable. Push tuples of (value, index, node), where index is the list number or a running counter, to break ties without comparing nodes. Interviewers notice when you handle this before it causes an error. Use a dummy head Start with a dummy node and a tail pointer. Appending becomes tail.next = node; tail = node, and you return dummy.next at the end. This removes special cases for the first node. Approach 2: Divide and conquer Pair up lists and merge each pair with the standard two-list merge, then repeat on the results. After log k rounds, one list remains. Each round touches all N nodes, so the time is also O(N log k), with O(1) extra space if done iteratively. This approach is a good follow-up answer and shows that you see the connection to merge sort. Why not merge one at a time? Merging list 1 with list 2, then the result with list 3, and so on, costs O(kN) in the worst case because early nodes are copied repeatedly. Explaining why this is slower demonstrates good complexity intuition. Testing Walk through a small example such as [1,4,5], [1,3,4], [2,6]. Also check an empty input, a single list, lists of very different lengths, and lists containing duplicates. Follow-up questions - What if the lists are streams that do not fit in memory? The heap approach works naturally as an external merge. - What if you need the k smallest elements only? Stop after k pops. - How does this relate to external sorting of large files? Sorted runs merged with a heap is the same idea. Communicating your approach State the baseline, explain why the heap improves it, and give the complexity before writing code. If you tend to freeze when an interviewer is watching, Linkjob AI (https://linkjob.ai) provides real-time help during job interviews and technical interviews, helping you stay organized while you code.
Created Nov 14, 2026 19:55 Expires Nov 14, 2027 19:55