Skip to content
Decoder

Merge k Sorted Lists

Hard

Problem

You get an array lists of k linked lists, and each list is sorted in ascending order. Merge them all into one sorted linked list and return its head. Any of the lists may be empty, and so may the array itself.

Example 1

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

Example 2

Input
lists = []
Output
(empty)

Example 3

Input
lists = [(empty)]
Output
(empty)

The tests pass lists as an array of list heads.

Constraints

  • 0 <= k <= 10^4
  • 0 <= length of each list <= 500
  • -10^4 <= Node.val <= 10^4
  • The total number of nodes is at most 10^4.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.