Why did my python code timeout for [1,2,3]?


  • 0
    A

    Could someone tell me why my code timeout for [1,2,3]? For other test cases it seems fine.

            if head == None or head.next == None or head.next.next == None:
                return head
    
            ol = head
            el = head.next
            eh = el
            n = el.next
            count = 3
            
            while n != None and n.next != None:
                if count % 2 == 1:
                    ol.next = n
                    n = n.next
                    ol = ol.next
                    count += 1
                else:
                    el.next = n
                    n = n.next
                    el = el.next
                    count += 1
            if count % 2 == 1:
                ol.next = n
                n.next = eh
            else:
                el.next = n
                ol.next = eh
            return head
    

  • 0

    @AceArthur the next of last evenNode should be null. take care of the next of the last node.


Log in to reply
 

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