Merge k Sorted Lists
Problem Statement
You get k linked lists. Each list is already sorted from smallest to largest.
Merge them into one sorted linked list that uses all of the nodes. Return the head of the merged list.
You may relink the existing nodes. You do not need to make copies of the values.
lists: an array of k linked-list heads. A list can be empty, and then its head is null.
The head of one sorted linked list that holds every node from every input list.
Three sorted lists with 8 nodes in total. The merged list holds all 8 nodes in order.
There are no lists, so there are no nodes to return.
The only list is empty.
- β’ k == lists.length
- β’ 0 β€ k β€ 10^4
- β’ 0 β€ lists[i].length β€ 500
- β’ β10^4 β€ lists[i][j] β€ 10^4
- β’ lists[i] is sorted in ascending order
- β’ The sum of lists[i].length will not exceed 10^4
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Copy all values into an array, sort it, then build a new list from the sorted values
Merge the result with each list in turn, using the two-list merge
Keep one head per list in a min-heap; pop the smallest and push the next node of that list
Ignore that the lists are already sorted. Walk every list and collect all values into one array, sort the array, and build a new list from the sorted values. With N total nodes, the sort costs O(N log N).
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like K-way Merge translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.