2. Add Two Numbers
  • Description
  • Brute-Force
    O(n)
    #!/usr/bin/python
    
    class ListNode(object):
        def __init__(self,val):
            self.val = val
            self.next = None # the pointer initially points to nothing
    
    def addTwoNumbers(l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        r = None
        rd = 0 # round
        while l1 != None or l2 != None:
            temp = rd
            if l1 != None:
                temp += l1.val
            if l2 != None:
                temp += l2.val
            if temp >= 10:
                temp -= 10
                rd = 1
            else:
                rd = 0
            if r == None:
                n = ListNode(temp)
                r = n
            else:
                m = ListNode(temp)
                n.next = m;
                n = m;
            if l1 != None:
                l1 = l1.next
            if l2 != None:
                l2 = l2.next
        if rd > 0:
            n.next = Node(1)
        return r
    
    def displayList(l):
        while l != None:
            print l.val,
            l = l.next
        print
    
    if __name__ == '__main__':
        n1 = ListNode(2)
        n2 = ListNode(4)
        n3 = ListNode(3); 
        n1.next = n2
        n2.next = n3
    
        l1 = n1
    
        n4 = ListNode(5)
        n5 = ListNode(6)
        n6 = ListNode(4)
        n4.next = n5
        n5.next = n6
    
        l2 = n4
    
        r = addTwoNumbers(l1, l2)
    
        displayList(l1)
        displayList(l2)
        displayList(r)
    			
    Revise
    O(n)
    #!/usr/bin/python
    
    class ListNode(object):
        def __init__(self,val):
            self.val = val
            self.next = None # the pointer initially points to nothing
    
    def addTwoNumbers(l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        r = ListNode(0) # initial round to be zero
        temp = r
        while l1 != None or l2 != None:
            temp.val = l1.val + l2.val + temp.val
            if temp.val >= 10:
                temp.val -= 10
                temp.next = ListNode(1)
            l1 = l1.next
            l2 = l2.next
            if l1 == None or l2 == None:
                break
            if temp.next == None:
                temp.next = ListNode(0)
            temp = temp.next
        if l1 != None:
            if temp.next != None:
                temp.next = addTwoNumbers(l1, temp.next)
            else:
                temp.next = l1
        if l2 != None:
            if temp.next != None:
                temp.next = addTwoNumbers(temp.next, l2)
            else:
                temp.next = l2
        return r
    
    def displayList(l):
        while l != None:
            print(l.val, end=', ')
            l = l.next
        print()
    
    if __name__ == '__main__':
        n1 = ListNode(2)
        n2 = ListNode(4)
        n3 = ListNode(3); 
        n1.next = n2
        n2.next = n3
    
        l1 = n1
    
        n4 = ListNode(5)
        n5 = ListNode(6)
        n6 = ListNode(4)
        n4.next = n5
        n5.next = n6
    
        l2 = n4
    
        r = addTwoNumbers(l1, l2)
    
        displayList(l1)
        displayList(l2)
        displayList(r)