Logo for ammarahmed.ca
Hard
Feb 18, 2026
#linked-list
#heap

23. 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:

1Input: lists = [[1,4,5],[1,3,4],[2,6]] 2Output: [1,1,2,3,4,4,5,6] 3Explanation: The linked-lists are: 4[ 5 1->4->5, 6 1->3->4, 7 2->6 8] 9merging them into one sorted linked list: 101->1->2->3->4->4->5->6

Example 2:

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

Example 3:

1Input: lists = [[]] 2Output: []
  • 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.

              Constraints:

              Notes

              Intuition

              We want to take the smallest value at a time from each list and only iterate further on that list after a value is taken from that list.

              Implementation

              We can use a min-heap alongside keeping track of the pointers in array and accessing them via index. To start, we create our array of pointers to the lists. At the same time, we add the first element of each non-empty list to the heap along with its index (with the value as the priority).

              After this, we iterate while the heap is not empty, we pop from the heap and add the new node to our resultant list. Using the index, we increment that specific pointer. Then, we check if the the updated pointer is not null (i.e. next is defined) and we push that value and its index to the heap. This will also ensure that whichever index is popped from the heap will be defined at that point.

              Finally, return the result.

              Edge-cases

              Ensure to push the first elements from the non-empty lists from the heap to start so that we have something to iterate over.

              • Time: O(N log k): where N is the total number of nodes in all k lists. The main iteration is the heap iteration which will run once for each node in the result, on each iteration, we pop and potentially push to the heap which is O(log k) because the size of the heap will never exceed the starting size will be at most k.
                • Space: O(k): The only extra space is the heap which will have, at most, k elements

                  Complexity

                  Solution

                  1from typing import List, Optional 2from lcutils import ListNode 3import heapq 4 5# Definition for singly-linked list. 6# class ListNode: 7# def __init__(self, val=0, next=None): 8# self.val = val 9# self.next = next 10class Solution: 11 def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]: 12 k = len(lists) 13 ptrs: List[Optional[ListNode]] = [None] * k 14 heap = [] 15 for i in range(k): 16 ptr = lists[i] 17 ptrs[i] = ptr 18 if ptr: 19 heapq.heappush(heap, (ptr.val, i)) 20 21 dummy = ListNode(0) 22 curr = dummy 23 while heap: 24 val, i = heapq.heappop(heap) 25 curr.next = ListNode(val) 26 curr = curr.next 27 # will be defined because it was added 28 ptrs[i] = ptrs[i].next 29 if ptrs[i]: 30 heapq.heappush(heap, (ptrs[i].val, i)) 31 return dummy.next 32 33 34 35if __name__ == "__main__": 36 # Include one-off tests here or debugging logic that can be run by running this file 37 # e.g. print(solution.two_sum([1, 2, 3, 4], 3)) 38 solution = Solution() 39 40