Share Python Code


  • 0
    W
    class Solution:
        # @return a ListNode
        def addTwoNumbers(self, l1, l2):
            if l1==None:
                return l2
            elif l2==None:
                return l1
            else:
                head=l1
                carryon=(l1.val+l2.val)/10
                l1.val=(l1.val+l2.val)%10
                while l1.next !=None and l2.next !=None:
                    carryon+=(l1.next.val+l2.next.val)
                    l1.next.val=carryon%10
                    carryon=carryon/10
                    l1=l1.next
                    l2=l2.next
                if l1.next == None:
                    l1.next = l2.next
                while l1.next != None:
                    carryon += l1.next.val
                    l1.next.val = carryon % 10
                    carryon = carryon // 10;
                    l1 = l1.next;
                if carryon > 0:
                    l1.next = ListNode(carryon)
    
                return head

Log in to reply
 

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