My python solution


  • 0
    J
    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def addTwoNumbers(self, l1, l2):
            """
            :type l1: ListNode
            :type l2: ListNode
            :rtype: ListNode
            """
            l = l1
            while l1 != None:
    
                l2 = ListNode(0) if l2==None else l2
    
                step = int((l1.val + l2.val)/10)
                l1.val = (l1.val + l2.val) % 10
                
                if step>0 or l2.next!=None:
                    l1.next = ListNode(0) if l1.next==None else l1.next
                    l1.next.val+=step
                        
                l1 = l1.next
                l2 = l2.next
    
            return l
    

Log in to reply
 

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