Merge k Sorted Lists
Updated
Problem statement
You are given an array lists containing k linked-list heads. Every linked list is sorted in ascending order.
Merge all of the linked lists into one ascending linked list and return its head.
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]The linked lists are:
1 -> 4 -> 5
1 -> 3 -> 4
2 -> 6Merging them produces the sorted linked list 1 -> 1 -> 2 -> 3 -> 4 -> 4 -> 5 -> 6.
Example 2
lists = []return = []Example 3
lists = [[]]return = []Constraints
k == lists.length0 <= k <= 10^40 <= lists[i].length <= 500-10^4 <= lists[i][j] <= 10^4- Each
lists[i]is sorted in ascending order. - The sum of all
lists[i].lengthvalues does not exceed10^4.
More ZS Onsite Interview questions
- Employees Whose Manager Left the CompanyONSITE INTERVIEW · Seen Sep 2026
- Merge Two Sorted Linked ListsONSITE INTERVIEW · Seen Sep 2026
- Fifth Highest Salary In A DepartmentONSITE INTERVIEW · Seen Sep 2026
- Find Duplicate Email AddressesONSITE INTERVIEW · Seen Sep 2026
- Top Salaries By DepartmentONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- LRU Cache with TTLAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Merge k Sorted ListsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Thread-Safe LRU Cache with TTLLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Merge K Sorted ListsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Merge K Sorted ListsSkydio · ONSITE INTERVIEWSkydio interview questions
- Running Median of a Multilevel Linked List in Level OrderNutanix · ONSITE INTERVIEWNutanix interview questions
- Greedy Person-Cake Matching DistanceSnowflake · PHONE SCREENSnowflake interview questions
- LRU Cache with Expiring EntriesPaytm · PHONE SCREENPaytm interview questions
- Merge K Sorted Linked ListsBharatPe · PHONE SCREEN
- Merge k Sorted ListsOracle · PHONE SCREENOracle interview questions