-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddTwoNumbers.py
61 lines (52 loc) · 1.04 KB
/
addTwoNumbers.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
# @return a ListNode
def addTwoNumbers(l1, l2):
head = None
l = None
carry = 0
while l1 or l2 or carry:
if l1:
val1 = l1.val
l1 = l1.next
else:
val1 = 0
if l2:
val2 = l2.val
l2 = l2.next
else:
val2 = 0
s = val1 + val2 + carry
carry = int(s / 10)
n = ListNode(s % 10)
if not head:
head = n
l = n
else:
l.next = n
l = n
return head
a = ListNode(2)
b = ListNode(4)
c = ListNode(3)
a.next = b
b.next = c
d = ListNode(5)
e = ListNode(6)
f = ListNode(4)
d.next = e
e.next = f
s = addTwoNumbers(a, d)
assert s.val == 7
assert s.next.val == 0
assert s.next.next.val == 8
assert s.next.next.next == None
g = ListNode(5)
h = ListNode(5)
s = addTwoNumbers(g, h)
assert s.val == 0
assert s.next.val == 1
assert s.next.next == None