Python: add two sum


  • 0
    H
    class Solution(object):
        def addTwoNumbers(self, l1, l2):
            """
            :type l1: ListNode
            :type l2: ListNode
            :rtype: ListNode
            """
            s, c = (l1.val + l2.val)%10, (l1.val + l2.val)/10
            l1, l2 = l1.next, l2.next
      
            rst = ListNode(s)
            
            rp = rst
            while l1 or l2:
                v1, v2 = 0, 0
                if l1:
                    v1, l1 = l1.val, l1.next
                if l2:
                    v2, l2 = l2.val, l2.next
                
                s, c = (v1 + v2 + c)%10, (v1 + v2 + c)/10
                rp.next = ListNode(s)
                rp = rp.next
                
            if c:
                rp.next = ListNode(c)
                
            return rst
    

  • 0
    G

    if will be a WA if l1 = [] , l2 = [1,2,3]


Log in to reply
 

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