# 每日一题：2.两数相加

### 每日一题：2.两数相加

##### 1、题目

示例 1：

输入：l1 = [2,4,3], l2 = [5,6,4]
输出：[7,0,8]
解释：342 + 465 = 807.
示例 2：

输入：l1 = [0], l2 = [0]
输出：[0]
示例 3：

输入：l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
输出：[8,9,9,9,0,0,0,1]

提示：

每个链表中的节点数在范围 [1, 100] 内
0 <= Node.val <= 9
题目数据保证列表表示的数字不含前导零

##### 2、解法

# 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:
val1 = l1.val
val2 = l2.val
quotient = 0
remainder = (val1 + val2 + quotient)%10
quotient = (val1 + val2 + quotient)//10
while l1.next is not None or l2.next is not None:
val1 = val2 = 0
if l1.next is not None:
l1 = l1.next
val1 = l1.val
if l2.next is not None:
l2 = l2.next
val2 = l2.val
sum1 = val1 + val2 + quotient
remainder = sum1 % 10
quotient = sum1 // 10
newNode = ListNode(remainder)
p.next = newNode
p = p.next

if quotient != 0:
p.next = ListNode(1)

return root
`

04-03 75
04-10 217
04-12 28
01-31 245