#23

Merge k Sorted Lists

Hard Min-heap of list heads

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Merge k sorted linked lists into one sorted linked list.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: merge k sorted lists.

Why this works: the next node is the smallest current head; a min-heap returns it in O(log k).

Example: [1→4→5, 1→3→4, 2→6]

heap {1, 1, 2} → take 1, push 4 …1→1→2→3→4→4→5→6

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Merging lists one after another into a growing result — O(N·k).

Solve on LeetCode ↗
Complexity
  • Time: O(N log k), N = total nodes.
  • Space: O(k).
Topic: Linked List
🎬 Episodes 40–45: video coming soon