Skip to content

Merge k Sorted Lists

HardLinked List
Decode it first. Clues, constraints, pattern and plan before any code.

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.

lists = [1 -> 4 -> 5, 1 -> 3 -> 4, 2 -> 6]   →  1 -> 1 -> 2 -> 3 -> 4 -> 4 -> 5 -> 6
lists = []                                   →  (empty)
lists = [(empty)]                            →  (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.