answer in python


  • 0
    L
    class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        flag=0
        a1=[]
        a2=[]
        a3=[]
        while l1:
            a1.append(l1.val)
            l1=l1.next
        while l2:
            a2.append(l2.val)
            l2=l2.next
        if len(a1)==len(a2):
            for i in range(len(a1)):
                a3+=[a1[i]+a2[i]+flag]
                if a3[i]>9:
                    a3[i]-=10
                    flag=1
                else:
                    flag=0
        elif len(a1)<len(a2):
            for i in range(len(a2)):
                if i<len(a1):
                    a3+=[a1[i]+a2[i]+flag]
                else:
                    a3+=[a2[i]+flag]
                if a3[i]>9:
                    a3[i]-=10
                    flag=1
                else:
                    flag=0
        else:
            for i in range(len(a1)):
                if i<len(a2):
                    a3+=[a1[i]+a2[i]+flag]
                else:
                    a3+=[a1[i]+flag]
                if a3[i]>9:
                    a3[i]-=10
                    flag=1
                else:
                    flag=0
        if flag==1:
            a3+=[1]
        return a3

Log in to reply
 

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