-
Notifications
You must be signed in to change notification settings - Fork 0
/
addTwoNumbers.py
59 lines (51 loc) · 1.39 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
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
"""
LeetCode No.2, add two numbers
76 ms,
13.8 MB
"""
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
carry = 0
prenode = ListNode(0)
lastnode = prenode
while carry or l1 or l2:
v1 = l1.val if l1 else 0
v2 = l2.val if l2 else 0
val = (v1 + v2 + carry) % 10
carry = (v1 + v2 + carry) // 10
lastnode.next = ListNode(val)
lastnode = lastnode.next
l1 = l1.next if l1 else None # shift to next node
l2 = l2.next if l2 else None
return prenode.next
def genList(l: list) -> ListNode:
prenode = ListNode(0)
lastnode = prenode
for val in l:
lastnode.next = ListNode(val)
lastnode = lastnode.next
return prenode.next
def printList(l: ListNode):
outstr = ''
while l:
outstr = outstr + str(l.val)
l = l.next
if l:
outstr = outstr + ' -> '
print(outstr)
if __name__ == '__main__':
app = Solution()
num1 = '123'
num2 = '4567'
l1 = [int(c) for c in num1[::-1]]
l2 = [int(c) for c in num2[::-1]]
a = genList(l1)
b = genList(l2)
printList(a)
printList(b)
printList(app.addTwoNumbers(a, b))