-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0002 - Add Two Numbers.py
41 lines (34 loc) · 1.17 KB
/
0002 - Add Two Numbers.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
# 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: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
carrybit = 0
l1p, l2p = l1, l2
r, rp = None, None
while l1p is not None and l2p is not None:
tmp = carrybit + l1p.val + l2p.val
carrybit = tmp // 10
node = ListNode(tmp % 10)
if r is None:
r, rp = node, node
else:
rp.next, rp = node, node
l1p, l2p = l1p.next, l2p.next
if l1p is None:
l1p = l2p
l2p = None
while l1p is not None:
tmp = carrybit + l1p.val
carrybit = tmp // 10
node = ListNode(tmp % 10)
if r is None:
r, rp = node, node
else:
rp.next, rp = node, node
l1p = l1p.next
if carrybit:
rp.next = ListNode(carrybit)
return r