Skip to content
AI360Xpert
Beta
Difficulty: HardLinked List

Merge k Sorted Lists

Problem in Plain English

You are given an array of k linked lists that are each sorted in ascending order. Merge them into one sorted linked list and return its head.

Problem Statement

You are given an array lists of k linked lists, where each list is sorted in ascending order and any entry may be empty.

Merge all the lists into one sorted linked list and return its head, reusing the existing nodes where possible. The total number of nodes across all lists is bounded, while k alone can be large.

Lists are terminating, independently owned chains. The rebuilding baseline creates fresh nodes and preserves every input. The pairwise approach splices original nodes and updates the lists array, so callers must allow those mutations.

Constraints

  • 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 all lists[i].length does not exceed 10^4.

Examples

Example 1

Input
lists = [[1,4,5],[1,3,4],[2,6]]
Output
[1,1,2,3,4,4,5,6]

The three sorted chains weave into 1 -> 1 -> 2 -> 3 -> 4 -> 4 -> 5 -> 6.

Example 2

Input
lists = []
Output
[]

No lists means the merged result is empty.

Example 3

Input
lists = [[]]
Output
[]

One empty list still merges to an empty result.

Example 4

Input
lists = [[],[-3,-1,-1],[],[-2,-1,4]]
Output
[-3,-2,-1,-1,-1,4]

Skip the empty chains, interleave the negative heads in ascending order, and retain all three occurrences of -1. Equal values belong to separate nodes and must not be deduplicated.

Intuition

The smallest remaining value of any sorted chain is at its head. Merging two chains repeatedly takes the smaller head, then attaches an already sorted remainder when one side ends. In Example 1, merging [1,4,5] with [1,3,4] gives [1,1,3,4,4,5]; merging that result with [2,6] gives the answer. Pair lists in balanced rounds so a node participates in at most logarithmically many merges, instead of being rescanned after every newly added list. Empty array entries still cost work to inspect, even when there are few or no nodes.

Approaches

Collect Sort Rebuild

Solution Details

Reveal Collect Sort Rebuild: intuition, complexity, and code

Hints

Hint 1
Pooling every value and sorting works but discards the per-list order, so keep it only as the baseline.
Hint 2
A two-list merge helper that splices nodes with a dummy head is the building block for every better plan.
Hint 3
Merge pairs in rounds with a doubling interval so k chains collapse in logarithmic rounds instead of one-by-one.

Edge Cases

  • An empty array of lists returns null without entering any merge round.
  • Entries that are empty lists are skipped naturally by the two-list helper.
  • One long list plus many empty ones carries the long list through untouched.
  • Duplicate and negative values retain every node and sort numerically; equal heads choose the right chain without deleting the left node.
  • An odd number of chains leaves one block unpaired in a round; interval doubling later brings it into slot 0.
  • At k=10000 with all heads null, merging still costs O(k); with one nonempty list, avoid assuming there are no head-array costs.

Common Mistakes and Interview Tips

  • Merging lists one after another into a growing accumulator, which repeats work and times out on large k.
  • Forgetting the empty-input guard and indexing lists[0] when no list exists.
  • Building fresh nodes in the helper instead of splicing, which wastes the reuse the optimal plan allows.
  • Using JavaScript's default lexicographic sort, which puts 10 before 2; the rebuilding code supplies a numeric comparator.
  • Claiming constant space while allocating a new Python array each round; the shown Python code updates the caller's array in place.
  • Expecting original chains to remain intact after splicing; use rebuilding or clone inputs if callers need preservation.
  • Passing chains that share nodes into the splicing helper can attach a node to itself; independent chain ownership is required.

Key Takeaway

When many sorted chains must become one, merge pairs in halving rounds so linear two-way merges stack into logarithmic depth.