FastPrepMerge K Sorted Lists

Merge K Sorted Lists

Skydio logoSkydio● HardFULLTIMEONSITE INTERVIEW
Learn

Problem statement

You are given k singly linked lists, each sorted in nondecreasing order. For the runner, lists[i] stores the node values of the i-th list from head to tail.

Merge all lists into one sorted linked list and return its values from head to tail. Reusing existing nodes or constructing an equivalent merged list are both acceptable.

Function

mergeKSortedLists(lists: int[][]) → int[]

Examples

Example 1

lists = [[1,4,5],[1,3,4],[2,6]]return = [1,1,2,3,4,4,5,6]

Taking the smallest available head at each step produces the complete nondecreasing merge.

Example 2

lists = []return = []

With no input lists, the merged list is empty.

Example 3

lists = [[],[-2,0,7],[]]return = [-2,0,7]

Empty lists contribute no nodes, so the one nonempty list is returned unchanged in value order.

Constraints

  • 0 <= lists.length <= 10000.
  • 0 <= lists[i].length <= 500.
  • The total number of values across all lists is at most 100000.
  • -10^9 <= lists[i][j] <= 10^9.
  • Every lists[i] is sorted in nondecreasing order.

More Skydio problems

See Skydio hiring insights
public int[] mergeKSortedLists(int[][] lists) {
    // Write your code here.
}
lists[[1,4,5],[1,3,4],[2,6]]
expected[1,1,2,3,4,4,5,6]
Checking account…