-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.py
43 lines (39 loc) · 1.12 KB
/
2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python3
# author lizhu
# date 2020-02-29
# update 2021-04-10
'''
leetcode-2 addTwoNumbers
'''
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
'''
第一:首先逆序不知如何解决
其实,先逆序求和,得到结果后在逆序回来和一开始正序求和的结果一样!!!
第二:两个列表的遍历有问题
先更新next
再更新val
'''
carry = 0
ret = ListNode()
cur = ret
while l1 or l2 or carry:
num = 0
if l1:
num += l1.val
l1 = l1.next
if l2:
num += l2.val
l2 = l2.next
if carry:
num += carry
carry -= 1
carry, num = divmod(num, 10)
cur.next = ListNode(num)
cur = cur.next
return ret.next # ret.val = 0