This question builds off the LC easy Merge 2 Sorted Lists. We use that as a helper function for this problem as well.
The main optimization we do here is merge pair of 2 LL’s at a time (similar to the merge operation in merge sort) which costs us O(logk) merges instead of an inefficient O(k) time complexity if we merge one by one (e.g. merge list i=1,2 and merge that result with i=3 and so on).
classSolution:defmergeKLists(self, lists: List[Optional[ListNode]])-> Optional[ListNode]:# base caseifnot lists orlen(lists)==0:returnNonewhilelen(lists)>1: merged =[]# skip by 2 to get pairs of lists# e.g. len(lists) = 9 so i in [0, 2, 4, 6, 8]for i inrange(0,len(lists),2): l1 = lists[i] l2 = lists[i +1]if(i +1)<len(lists)elseNone merged.append(self.mergeLists(l1, l2)) lists = merged
return lists[0]defmergeLists(self, l1, l2): dummy = ListNode() tail = dummy
while l1 and l2:if l1.val > l2.val: tail.next= l2
l2 = l2.nextelse: tail.next= l1
l1 = l1.next tail = tail.nextif l1: tail.next= l1
else: tail.next= l2
return dummy.next