CodexBloom - Programming Q&A Platform

Issues with Efficiently Merging k Sorted Lists in Python - Time Complexity Concern

👀 Views: 50 💬 Answers: 1 📅 Created: 2025-06-12
python algorithms data-structures linked-lists

I'm stuck on something that should probably be simple. After trying multiple solutions online, I still can't figure this out. After trying multiple solutions online, I still can't figure this out. I'm currently working on a problem where I need to merge k sorted linked lists into one sorted linked list. I've implemented a solution using a priority queue (heap) from the `heapq` library, but I'm running into performance issues with larger inputs. My code looks something like this: ```python import heapq class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def mergeKLists(lists): min_heap = [] for i, l in enumerate(lists): if l: heapq.heappush(min_heap, (l.val, i, l)) # Push the node value and list index dummy = ListNode(0) current = dummy while min_heap: val, index, node = heapq.heappop(min_heap) current.next = node current = current.next if node.next: heapq.heappush(min_heap, (node.next.val, index, node.next)) return dummy.next ``` I've tested this function with a few examples, but when I pass in a large number of lists (e.g., 1000 lists with each having 1000 nodes), it takes an unexpectedly long time to process. I’m particularly concerned about the time complexity here. According to my understanding, the overall time complexity should be O(N log k), where N is the total number of nodes and k is the number of linked lists. However, the actual performance seems much worse, and I'm not sure if I'm missing something in the implementation or if there are better ways to optimize this. I also tried using a divide and conquer approach, merging pairs of lists iteratively, but that didn’t yield significant improvements. Here's a snippet of that attempt: ```python def mergeTwoLists(l1, l2): dummy = ListNode(0) tail = dummy while l1 and l2: if l1.val < l2.val: tail.next = l1 l1 = l1.next else: tail.next = l2 l2 = l2.next tail = tail.next tail.next = l1 or l2 return dummy.next def mergeKListsDivideAndConquer(lists): if not lists: return None while len(lists) > 1: merged_lists = [] for i in range(0, len(lists), 2): l1 = lists[i] l2 = lists[i + 1] if i + 1 < len(lists) else None merged_lists.append(mergeTwoLists(l1, l2)) lists = merged_lists return lists[0] ``` But again, the performance remains subpar. I’m looking for insights into potential bottlenecks in my implementation or alternative strategies that may lead to better performance. Any tips would be greatly appreciated! For context: I'm using Python on Linux. Has anyone else encountered this? Has anyone else encountered this?