DC python solution O(nk log k) runtime, O(1) space


  • 3
    T
    def mergeKLists(self, lists):
        if not lists:
            return None
        start = 0
        end = len(lists) - 1
        while start != end or end != 0:
            if start >= end:
                start = 0
            else:
                lists[start] = self.mergeTwoLists(lists[start], lists[end])
                start += 1
                end -= 1
        return lists[0]
    
    def mergeTwoLists(self, l1, l2):
        current = head = ListNode(0)
        while l1 and l2:
            if l1.val <= l2.val:
                current.next = l1
                l1 = l1.next
            else:
                current.next = l2
                l2 = l2.next
            current = current.next
        current.next = l1 or l2
        return head.next

  • 0
    V

    Why would this O(n*klogk) runtime? I see no evidence of splitting by halves


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.