forked from buy/cc150
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.5.add_two_numbers.py
71 lines (52 loc) · 1.25 KB
/
2.5.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
def __str__(self):
output = []
while self:
output.append(str(self.data))
self = self.next
return ' -> '.join(output)
def add(nodes1, nodes2):
carry = 0
output = None
while nodes1 and nodes2:
if not output:
output = Node((nodes1.data + nodes2.data) % 10)
temp = output
else:
temp.next = Node((carry + nodes1.data + nodes2.data) % 10)
temp = temp.next
if (nodes1.data + nodes2.data) / 10 > 0:
carry = 1
else:
carry = 0
nodes1 = nodes1.next
nodes2 = nodes2.next
while nodes1:
temp.next = Node((carry + nodes1.data) % 10)
temp = temp.next
if ((carry + nodes1.data) / 10) > 0:
carry = 1
else:
carry = 0
nodes1 = nodes1.next
while nodes2:
temp.next = Node((carry + nodes2.data) % 10)
temp = temp.next
if ((carry + nodes2.data) / 10) > 0:
carry = 1
else:
carry = 0
nodes2 = nodes2.next
if carry:
temp.next = Node(1)
return output
if __name__ == '__main__':
ls1 = Node(7)
ls2 = Node(5)
ls2.next = Node(9)
ls2.next.next = Node(2)
print '`{ls1}` + `{ls2}`'.format(ls1=ls1, ls2=ls2)
print add(ls1, ls2)