TLE with Brute Force Method when the list is empty

  • 0

    Here is my Brute Force solution with Python. However, OJ indicated time limit error when the input is [[]]. I was expecting the Brute Force method would exceed the time limit, but definitely not in this case. Is there anything wrong with my code?

    class Solution:
    # @param {ListNode[]} lists
    # @return {ListNode}
    def mergeKLists(self, lists):
        k = len(lists)
        dummy = ListNode(None)
        i = 0
        curr_head = dummy
       # merge the k lists one by one
        while i < k:
            head = dummy
            p = curr_head
            q = lists[i]
            # merge two lists
            while p is not None and q is not None:
                if p.val <= q.val:
           = p
                    p =
           = q
                    q =
                head =
            if p is not None:
       = p
            if q is not None:
       = q
            curr_head =
            i += 1

Log in to reply

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