Python solution that doesn't compile


  • 0
    Z

    class Solution(object):
    def mergeKLists(self, lists):
    """
    :type lists: List[ListNode]
    :rtype: ListNode
    """

        def mergeHelper(lists):
            if len(lists) == 0:
                return []
            elif len(lists) == 1:
                return lists[0]
            else:
                return mergeTwo(mergeHelper(lists[0:len(lists)/2]),
                                mergeHelper(lists[len(lists)/2:len(lists)]))
        
        def mergeTwo(list1, list2):
            ind1, ind2 = 0, 0
            new_list = []
            while ind1 < len(list1) or ind2 < len(list2):
                if ind1 < len(list1) and (ind2 == len(list2) or list1[ind1] < list2[ind2]):
                    new_list.append(list1[ind1])
                    ind1 += 1
                else:
                    new_list.append(list2[ind2])
                    ind2 += 1
            return new_list
        
        return mergeHelper(lists)

Log in to reply
 

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