-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMicrosoft_Problem_12.py
79 lines (54 loc) · 1.56 KB
/
Microsoft_Problem_12.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
70
71
72
73
74
75
76
77
78
79
"""
This problem was asked by Microsoft.
Let's represent an integer in a linked list format by having each node represent a digit in the number. The nodes make up the number in reversed order.
For example, the following linked list:
1 -> 2 -> 3 -> 4 -> 5
is the number 54321.
Given two linked lists in this format, return their sum in the same linked list format.
For example, given
9 -> 9
5 -> 2
return 124 (99 + 25) as:
4 -> 2 -> 1
"""
class Node:
def __init__(self, val, next=None):
self.val = val
self.next = next
def _linked_list_to_value(head):
if head is None:
return
value = ""
current = head
while current:
value += "".join(str(current.val))
current = current.next
return int(value[::-1])
def _value_to_linked_list(value):
value = str(value)[::-1]
head = Node(value[0])
for i in range(len(value)):
if i == 0:
pass
else:
_append_node(head, value[i])
return head
def _append_node(head, value):
current = head
while current:
if current.next is None:
current.next = Node(value)
break
current = current.next
def display_linked_list(head):
if not head:
return
print(head.val)
display_linked_list(head.next)
def two_sum(head, head2):
value = _linked_list_to_value(head) + _linked_list_to_value(head2)
return _value_to_linked_list(value)
# Driver code
linked_list_1 = Node(9, Node(9))
linked_list_2 = Node(5, Node(2))
display_linked_list(two_sum(linked_list_1, linked_list_2))