LeetCode-Linked


2. Add Two Numbers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
ret = ListNode(0)
p = ret
r = 0
while(r or l1 or l2):
a1 = 0 if not l1 else l1.val
l1 = None if not l1 else l1.next
a2 = 0 if not l2 else l2.val
l2 = None if not l2 else l2.next
s = a1 + a2 + r
r = s // 10
p.next = ListNode(s % 10)
p = p.next
return ret.next
作者

Cheng

发布于

2020-01-13

更新于

2022-08-06

许可协议

评论