/
problem3_2.py
60 lines (50 loc) · 1.3 KB
/
problem3_2.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
res = 0
def reverse_linked_list(root):
if not root:
return None
if not root.next:
return root
curr, prev, next = root, None, root.next
while curr:
curr.next = prev
prev = curr
curr = next
if curr:
next = curr.next
return prev
def print_linked_list(root):
while root:
print(root.data)
root = root.next
def add_nodes(root, root2, mod, ind, result):
if not root and not root2:
if mod == 0:
# print(result)
global res
res = result
sum = mod
if root and root2:
sum = root.data + root2.data + mod
elif root:
sum = root.data + mod
elif root2:
sum = root2.data + mod
if sum > 0:
new_mod = sum // 10
result = result + (sum % 10) * 10 ** ind
add_nodes(root.next if root else None, root2.next if root2 else None, new_mod, ind+1, result)
class Node:
def __init__(self, data, next=None):
self.data = data
self.next = next
node2 = Node(9)
node1 = Node(9, node2)
root = Node(9, node1)
node4 = Node(2)
node3 = Node(3, node4)
root2 = Node(5, node3)
#print_linked_list(root)
#print_linked_list(root2)
result = 0
add_nodes(reverse_linked_list(root), reverse_linked_list(root2), 0, 0, result)
print(res)