This practice workspace is built for desktop. Open it on a larger screen to start solving.
Merge k Sorted Lists
You are given an array of k sorted linked lists. Merge them into a single sorted linked list and return it.
Input/output convention for this exercise: for portability, lists are passed in and out as plain arrays of values. The function receives an array of arrays (each inner array is a sorted list) and must return the merged result as a single array of values in non-decreasing order. The provided starter includes ListNode, arrayToList, listToArray, and arraysToLists helpers — use them to convert into linked-list form, run a real list-merge with a min-heap, then convert back.
A min-heap of (value, list-index) pairs gives the standard O(N log k) solution.
Examples
Constraints
- 0 <= k <= 10^4
- 0 <= lists[i].length <= 500
- -10^4 <= lists[i][j] <= 10^4
- Each lists[i] is sorted in non-decreasing order.
- The total length of all lists is at most 10^4.
Preview Mode
This is a read-only preview of DSAMind's AI coaching. Sign up to get custom feedback on your own solution.
Your approach
- Pattern:Heap
- Time:O(N)
- Space:O(1)
Complete approach — pattern and complexity both named.
Coach
What you got right
You correctly matched the sorted input requirement and implemented a linear-time scan using two pointers.
Where it diverged
No divergence detected. The code correctly aligns with the stated approach.
Next attempt: focus on
Try solving related sliding window problems to build familiarity with two-pointer variants.
Signals
No signals fired — clean run, you stayed in flow.