Need help time limit exceeded on small inputs


  • 0
    T
    def partition(self, head, x):
            smallhead = None
            bigequalhead = None
            real = None
            small = None
            while head != None:
                if head.val < x:
                    if smallhead == None:
                        smallhead = head
                        small = head
                    else:
                        smallhead.next = head
                        smallhead = head
                else:
                    if bigequalhead == None:
                        bigequalhead = head
                        real = head
                    else:
                        bigequalhead.next = head
                        bigequalhead = head
                head = head.next
            if small == None:
                return real
            smallhead.next = real
            return small
    

    with input [2,1] 2 the judge says time limit exceeded. Have no clue. Thanks.


  • 0
    U

    @themasterli I am facing the same problem. Did you figure out why?


  • 2
    B

    you have to maintain the tail node ( tail->next = NULL ).


  • 0
    Z

    set tailNode.next = None before you return code


  • 0
    S

    @bruce01j I did not understand why i should put the tail.next = null
    could you explain it to me ?
    thanks a lot


Log in to reply
 

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