Skip to content
Onsite

Merge K Sorted Lists

HardP1Coding

You are given an array lists of k singly linked lists, each sorted in ascending order. Merge them into one sorted linked list and return its head. lists may be empty, and any list in it may be empty.

A list node has val and next. The ListNode class is already defined for you.

Example 1

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

Example 2

Input
lists = []
Output
[]

Example 3

Input
lists = [[]]
Output
[]

Constraints

  • 0 <= k <= 10^4
  • 0 <= length of each list <= 500
  • Total number of nodes is at most 10^4.
  • -10^4 <= Node.val <= 10^4
  • Each list is sorted in ascending order.

Hints

Run checks the 3 visible tests, Submit all 8.