Merge K Sorted Linked Lists
Updated
Problem 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.
More BharatPe Phone Screen questions
Similar questions at other companies
- Merge K Sorted ListsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Merge Timestamped Record ListsNuro · PHONE SCREENNuro interview questions
- Sort a Linked ListAdobe · PHONE SCREENAdobe interview questions
- Merge K Sorted ListsSkydio · ONSITE INTERVIEWSkydio interview questions
- Sort a Linked List in Descending OrderMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- IPOThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
- Greedy Person-Cake Matching DistanceSnowflake · PHONE SCREENSnowflake interview questions
- Find Median from Data StreamSusquehanna International Group (SIG) · PHONE SCREENSusquehanna International Group (SIG) interview questions
- Median Across Sorted ArraysGoogle · PHONE SCREENGoogle interview questions
- Document Indexer Scheduling MetricsGlean · PHONE SCREENGlean interview questions