# 一、第2题

## 3.代码展示

def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
if not l1 and not l2: return None
move1 = l1
move2 = l2
dummy = ListNode(0)
move = dummy
mark = 0
while l1 and l2:
t = l1.val + l2.val + mark
if t >= 10:
n = t%10
mark = 1
else:
n = t%10
mark = 0
move.next = ListNode(n)
move = move.next
l1 = l1.next
l2 = l2.next
while l1:
t = l1.val + mark
if t >= 10:
n = t%10
mark = 1
else:
n = t%10
mark = 0
move.next = ListNode(n)
move = move.next
l1 = l1.next
while l2:
t = l2.val + mark
if t >= 10:
n = t%10
mark = 1
else:
n = t%10
mark = 0
move.next = ListNode(n)
move = move.next
l2 = l2.next
if mark == 1:
move.next = ListNode(1)
move = move.next
return dummy.next


# 二、第445题

## 2.思路介绍

1.将l1和l2反转。
2.将l1的值加l2的值得dummy。
3.反转dummy

## 3.代码展示

class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
def reverse(n):
pre,cur = None,n
while cur:
cur.next,pre,cur = pre,cur,cur.next
return pre
l1 = reverse(l1)
l2 = reverse(l2)
dummy = ListNode(0)
move = dummy
mark = 0
#遍历l1和l2
while l1 and l2:
t = l1.val + l2.val + mark
if t >= 10:
n = t%10
mark = 1
else:
n = t%10
mark = 0
move.next = ListNode(n)
move = move.next
l1 = l1.next
l2 = l2.next
#遍历l1
while l1:
t = l1.val + mark
if t >= 10:
n = t%10
mark = 1
else:
n = t%10
mark = 0
move.next = ListNode(n)
move = move.next
l1 = l1.next
#遍历l2
while l2:
t = l2.val + mark
if t >= 10:
n = t%10
mark = 1
else:
n = t%10
mark = 0
move.next = ListNode(n)
move = move.next
l2 = l2.next
#查漏
if mark == 1:
move.next = ListNode(1)
move = move.next
dummy.next = reverse(dummy.next)
return dummy.next


# 续总结两数相加Ⅰ和Ⅱ

## 两数相加反省

1.while l1 or l2 or s

2.l1.val if l1 else 0

## 两数相加Ⅱ反省

1.利用了栈的存储特点，只能在一边进出，存储两个数。
2.一个大佬将链表转换为了数字，然后将数字相加，最后转换为字符串输出，这就是大佬嘛，i了i了呀。

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

strive_2021

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文

03-26 3243
11-26 52
01-16 323
05-07 72
09-18 3891
07-10 29
12-16 2016