题目
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。 您可以假设除了数字 0 之外,这两个数都不会以 0 开头。 测试用例
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807
输入:(5) + (5) 输出:0 -> 1 原因:5 + 5 = 10
输入:(2 -> 4 -> 6) + (5 -> 6) 输出:7 -> 0 -> 7 原因:642 + 65 = 707
LeetCode平台实现方法一:两条链表同时遍历,遇到进位就为下一个进位的值+1
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
if isinstance(l1,list):
l1 = ListNode(l1)
l2 = ListNode(l2)
re = ListNode(0)
r=re
carry=0
while(l1 or l2):
x= l1.val if l1 else 0
y= l2.val if l2 else 0
s=carry+x+y
carry=s//10
r.next=ListNode(s%10)
r=r.next
if(l1!=None):l1=l1.next
if(l2!=None):l2=l2.next
if(carry>0):
r.next=ListNode(1)
return re.next
方法二 该代码来源LeetCode高赞答案
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
dummy = p = ListNode(None) #保存头结点,返回结果
s = 0 #每一步的求和暂存变量
while l1 or l2 or s: #循环条件:l1 或者l2(没有遍历完成),s(进位)不为0
s += (l1.val if l1 else 0) + (l2.val if l2 else 0) #这其实是好多代码,我自己写了好多行,但是作者这样写非常简洁,赞
p.next = ListNode(s % 10) #构建新的list存储结果,其实用较长的加数链表存也可以,%10:求个位
p = p.next
s //= 10 #求进位
l1 = l1.next if l1 else None
l2 = l2.next if l2 else None
return dummy.next
本地IDE实现
#
# @lc app=leetcode.cn id=2 lang=python3
#
# [2] 两数相加
#
# @lc code=start
# Definition for singly-linked list.
class ListNode():
def __init__(self, val):
if isinstance(val,int):
self.val = val
self.next = None
elif isinstance(val,list):
self.val = val[0]
self.next = None
cur = self
for i in val[1:]:
cur.next = ListNode(i)
cur = cur.next
def gatherAttrs(self):
return ", ".join("{}: {}".format(k, getattr(self, k)) for k in self.__dict__.keys())
def __str__(self):
return self.__class__.__name__+" {"+"{}".format(self.gatherAttrs())+"}"
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
if isinstance(l1,list):
l1 = ListNode(l1)
l2 = ListNode(l2)
re = ListNode(0)
r=re
carry=0
while(l1 or l2):
x= l1.val if l1 else 0
y= l2.val if l2 else 0
s=carry+x+y
carry=s//10
r.next=ListNode(s%10)
r=r.next
if(l1!=None):l1=l1.next
if(l2!=None):l2=l2.next
if(carry>0):
r.next=ListNode(1)
return re.next
# @lc code=end
if __name__ == "__main__":
test = Solution()
print(test.addTwoNumbers([1,3],[2,1,3]))