-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_two_numbers.py
43 lines (37 loc) · 1.06 KB
/
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
42
43
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
tonext = 0
head = None
prev = None
while True:
if not l1 and not l2 and tonext == 0:
break
a = l1.val if l1 else 0
b = l2.val if l2 else 0
d = a + b + tonext
#print("a = {}, b = {}, d = {}".format(a, b, d))
if d >= 10:
tonext = 1
d -= 10
else:
tonext = 0
current = ListNode(d)
if prev:
prev.next = current
else:
head = current
prev = current
current = current.next
if l1: l1 = l1.next
if l2: l2 = l2.next
return head