力扣https://leetcode-cn.com/problems/add-two-numbers/
leetcode 第一题 两数相加 -- 中等
直接相加就可以了,本来就是倒序的,不要考虑进位,s//10 后的值就是进位,自动参与到下一次中的运算.
class Solution:
def AddTwoNumber(self,L1,L2):
dummpy=p=ListNode()
s=0
while l2 or l2 or s != 0:
s += (l1.val if l1 else 0) + (l2.val if l2 else 0)
p.next = ListNode(s%10)
p=p.next
if l1:
l1=l1.next
if l2:
l2=l2.next
s= s//10
return dummpy.next
if __name__ == "__main__":
L1=[2,4,3]
L2=[5,5,4]
print(Solution().AddTwoNumber(L1,L2))