# Solved! Leetcode 23. Merge k Sorted Lists

### Merge k Sorted Lists

You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list and return it.

Example 1:
Input: lists = [[1,4,5],[1,3,4],[2,6]]
Output: [1,1,2,3,4,4,5,6]
[
1->4->5,
1->3->4,
2->6
]
merging them into one sorted list:
1->1->2->3->4->4->5->6

Example 2:
Input: lists = []
Output: []

Example 3:
Input: lists = [[]]
Output: []

Constraints:

• k == lists.length
• 0 <= k <= 104
• 0 <= lists[i].length <= 500
• -104 <= lists[i][j] <= 104
• lists[i] is sorted in ascending order.
• The sum of lists[i].length will not exceed 104.

### Time Complexity

• Let’s the size of the lists is m
• Let’s the size of each LinkedList (lists[i]) is n
• At any point of time the size of the min heap will be m or less
• To populate the min heap -> O(mlogm)
• remove a node from the min heap -> O(1) * n -> O(n)
• Add a new node n-times to the min heap -> O(nlogm)
• total -> O(mlogm + n + nlogm) -> O(nlogm)

### Space Complexity

• O(m): size of the min heap
Rate this post