ntree.ai
ntree.aiMerge k Sorted Lists

Merge k Sorted Lists

hardsolvedattempted

You are given an array lists of k linked lists, each sorted in ascending order.

Merge them into one sorted linked list and return its head.

Example 1:

Input:  lists = [[1, 4, 5], [1, 3, 4], [2, 6]]
Output: [1, 1, 2, 3, 4, 4, 5, 6]
Explanation: the three lists woven together in order.

Example 2:

Input:  lists = []
Output: []

Constraints:

  • 0 <= k <= 10^4
  • 0 <= length of each list <= 500
  • -10^4 <= Node.val <= 10^4
  • each list is sorted in ascending order
  • the total number of nodes <= 10^4
# Definitions:
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
 
def mergeKLists(lists: list[ListNode]) -> ListNode:
pass
 

Press Visualise to step through your code.