leetcode

输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
缘由:342 + 465 = 807

 

 

class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution(object):
    def addTwoNumbers(self, l1, l2):
        l1_list = []
        l2_list = []
        while l1:
            l1_list.append(l1.val)
            l1 = l1.next
        while l2:
            l2_list.append(l2.val)
            l2 = l2.next
       
        nums_list=[]
        tmp = 0
        while len(l1_list)<len(l2_list):
            l1_list.append(0)
            
        while len(l1_list) > len(l2_list): 
            l2_list.append(0)
            
        tmp =0
        for i in range(0,len(l1_list)):
                a = l1_list[i] + l2_list[i]
                if a >=10:
                    b=a-10+tmp
                    nums_list.append(b)
                    tmp=a//10
                else:
                    if a+tmp>=10:
                        b=a+tmp-10
                        tmp = (a+tmp)//10
                        nums_list.append(b)
                    else:
                        nums_list.append(a+tmp)
                        tmp=0
        if tmp>0:
            nums_list.append(tmp)
            
        
        return nums_list