Skip to main content
Create your own
Lesson illustration

K-way Merge: Combining Sorted Lists

Following our exploration of the Top K Elements pattern, we'll now delve into another powerful heap-based technique that is fundamental to distributed and large-scale data processing. In the last lesson, we saw how a min-heap could efficiently maintain a collection of the "Top K" largest items. Today, we will leverage that same data structure to solve a different, but related, problem.

This lesson focuses on the K-way Merge pattern. The objective is to efficiently merge k already sorted lists into a single, comprehensive sorted list. This scenario is incredibly common in scalable systems. Imagine needing to combine sorted search results from multiple database shards, merge sorted log files from a cluster of servers, or perform an external sort on a dataset too large to fit in memory. Mastering this pattern is a direct step toward designing the high-performance systems you're aiming to build.

1. From Two Lists to K Lists

You are likely familiar with merging two sorted lists, a core component of the Merge Sort algorithm. The process is straightforward: using two pointers, you compare the elements at the head of each list, take the smaller one, and advance the corresponding pointer.

But how do you generalize this to k lists? A naive approach might be to iteratively find the minimum element by scanning the current head of all k lists, adding it to your result, and advancing that list's pointer. For each of the N total elements, this would require k-1 comparisons, leading to a time complexity of O(N * k). This works, but it's inefficient, especially as k grows.

We need a more intelligent way to answer the question: "Among the k elements I'm currently looking at, which one is the smallest?" This should sound familiar. In our last lesson, we needed an efficient way to find the smallest element in our set of "top" items. The solution then, as it is now, is the min-heap.

The following video provides an excellent intuitive leap from merging two arrays to merging k arrays, and introduces the central challenge that the heap solves.

Merge K Sorted Arrays - Min Heap Algorithm ("Merge K Sorted Lists" on LeetCode)

This video from Back To Back SWE builds the foundation for the K-way merge algorithm.

Watch the first part of the video, from the introduction to the two-array merge example, and then continue through the generalization to k-arrays. Focus on the core logic: at every step, we only care about the smallest available element from each list.

2. The Heap-Based K-way Merge Algorithm

The key insight is to use a min-heap to keep track of only the "frontier" of elements—that is, the current smallest element from each of the k lists. The heap's structure ensures that the globally smallest element across all lists is always at the top, available in O(1) time.

The algorithm proceeds as follows:

  1. Initialization: Create a min-heap and insert the first element from each of the k non-empty input lists. Since you need to know which list an element came from to advance its pointer later, you'll typically store a tuple or an object in the heap, like (value, list_index). For linked lists, you can just store the node pointer itself.
  2. Extraction and Insertion Loop:
    • While the min-heap is not empty, extract the element with the minimum value. This is the smallest element across all lists currently under consideration.
    • Add this minimum element to your result list.
    • If the list from which you just extracted the element has more items, insert the next item from that same list into the heap.

This process is repeated until all elements from all lists have been processed, and the heap becomes empty.

The image below illustrates the initial state of this process, with four sorted linked lists and a priority queue (our min-heap) populated with the head of each list.

Four sorted linked lists are shown on the left. On the right, a priority queue (min-heap) is initialized with the head node from each of the four lists. The heap is ordered by the node's value, so the two nodes with value '1' are at the top.

Now, let's watch a detailed walkthrough of this exact process.

Merge K Sorted Arrays - Min Heap Algorithm ("Merge K Sorted Lists" on LeetCode)

The same video from Back To Back SWE now demonstrates the min-heap solution in action.

Watch the segment from min-heap approach. This part visually traces the algorithm, showing elements being added to the heap, the minimum being extracted, and the next element from the corresponding list being added back in.

3. Implementation and Complexity

This pattern is most famously represented by the "Merge K Sorted Lists" interview problem, where the input is an array of sorted linked lists. Let's examine a detailed guide on implementing the solution.

23. Merge k Sorted Lists - In-Depth Explanation

This article from Algo.monster provides a comprehensive breakdown of the problem, the heap-based solution, and a step-by-step walkthrough. It's an excellent resource for solidifying your understanding.

Please read the following sections: Intuition: This reinforces why a min-heap is the perfect tool for this job. Solution Approach: This provides the precise implementation steps, from enabling node comparison in languages like Python to using a dummy node for clean list construction. Example Walkthrough: Follow this concrete example to see the heap's state change at each iteration.

As explained in the resources, the complexity of this optimal approach is a significant improvement over the naive methods.

  • Time Complexity: , where N is the total number of elements across all lists, and k is the number of lists.

    • Every single one of the N elements must be inserted into and extracted from the heap once.
    • Each heap operation (insertion or extraction) takes time, as the heap's size is at most k.
  • Space Complexity:

    • The primary auxiliary space is for the min-heap, which stores at most k elements at any given time (one from each list). The O(N) space for the final output list is typically not counted as auxiliary space.

This space complexity is what makes the pattern so valuable for scalable systems. You can merge terabytes of data spread across k sorted files on disk using only enough memory to hold k elements.

4. System Design Application: External Sort

The most direct application of K-way merge in system design is external sorting. When you need to sort a file that is too large to fit into RAM, you can't just load it and call a standard sort function. Instead, you perform an external sort:

  1. Chunk and Sort: Read the large file in chunks that can fit into memory (e.g., 1GB chunks). Sort each chunk individually and write the sorted chunk back to disk as a temporary file.
  2. Merge: You now have k sorted temporary files on disk. You can use the K-way merge algorithm to merge these k files into a single, final sorted file. Your "pointers" are file handles, and your "heap" holds the next available line/record from each of the k files.

This technique allows you to sort virtually limitless amounts of data with a fixed amount of RAM, a cornerstone of large-scale data processing pipelines.

K-Way Merge: A Complete Guide for Beginners

This article provides a nice summary of the external sort application.

Read the section External Sort with K-Way Merge to connect the algorithm directly to this critical system design concept.

Conclusion

Today, we've dissected the K-way Merge pattern, a versatile and efficient technique for combining sorted data streams. It's a natural extension of our work with heaps and a prime example of how algorithmic patterns directly translate into solutions for large-scale engineering challenges.

Key Takeaways:

  • Problem: Merge k sorted lists into a single sorted list.
  • Optimal Solution: Use a min-heap to efficiently track the smallest element among the current heads of all k lists.
  • Complexity: The heap-based approach achieves a time complexity of and an auxiliary space complexity of .
  • System Design Relevance: This pattern is the foundation of external sorting and is widely used in distributed systems to merge results from multiple sorted sources like database shards or log files.

In our next lesson, we'll continue our journey with heaps by examining the Two Heaps pattern. This clever technique uses two heaps (one min-heap and one max-heap) to solve problems like finding the median of a continuously streaming data feed.

Can't find a good explanation? Sign up and we'll make it for you

Sign up