Merge K Sorted Lists
Updated
Problem statement
You are given k singly linked lists, each sorted in nondecreasing order. Here, lists[i] holds the node values of the i-th list from head to tail.
Merge all the lists into one sorted linked list and return its values from head to tail. You may reuse the existing nodes or build a new merged list.
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]Each time, take the smallest front value among the lists. That gives one, one, two, three, four, four, five, six, which is the full merged list in order.
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 interview questions
Similar questions at other companies
- Merge K Sorted ListsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Merge K Sorted Linked ListsBharatPe · PHONE SCREEN
- LRU Cache with TTLAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Merge k Sorted ListsZS · ONSITE INTERVIEWZS interview questions
- Merge k Sorted ListsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Thread-Safe LRU Cache with TTLLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Merge K Sorted Linked ListsThe Walt Disney Company · OA · ONSITE INTERVIEWThe Walt Disney Company interview questions
- Reverse Nodes in k-GroupMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Reverse a Linked List in Groups of KExpedia · ONSITE INTERVIEWExpedia interview questions
- Merge K Sorted Linked Lists Into OneCapital One · ONSITE INTERVIEWCapital One interview questions