Problem · Linked List
Merge K Sorted Linked Lists
Learn this problemProblem statement
You are given an array lists of k singly linked lists. Every linked list is sorted in nondecreasing order.
Merge all nodes into one linked list sorted in nondecreasing order and return its head. You may relink the existing nodes.
Function
mergeKLists(lists: ListNode[]) → ListNodeExamples
Example 1
lists = [[1,4,5],[1,3,4],[2,6]]return = [1,1,2,3,4,4,5,6]Taking the smallest current head repeatedly produces the combined sorted order.
Example 2
lists = []return = []There are no nodes to merge.
Example 3
lists = [[],[-2,0,7],[3]]return = [-2,0,3,7]Empty lists contribute no nodes; the remaining two lists merge in sorted order.
Constraints
0 <= k <= 100- The total number of nodes across all lists is at most
5000. -10^9 <= node.val <= 10^9- Every input list is sorted in nondecreasing order.