Why its time limit exceeded


  • 0
    Y

    carry = 0
    ptr = ListNode(0)
    head = ptr

        while True:
            n = l1.val + l2.val + carry
            ptr = ListNode(n) if n <10 else ListNode(n %10)
            carry = 0 if n<10 else 1
            
            
            if l1.next ==None and l2.next == None:
                if carry == 1:
                    node = ListNode(1)
                    ptr.next = node
                break
                
            if l1.next == None and l2.next !=None:
                l1.val = 0
                l2 = l2.next
                
                
            if l1.next != None and l2.next == None:
                l2.val = 0
                l1 = l1.next
           
            
            ptr = ptr.next
        
        return head.next

Log in to reply
 

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