-
Notifications
You must be signed in to change notification settings - Fork 0
/
#20.py
55 lines (42 loc) · 1.02 KB
/
#20.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
class Node:
def __init__(self, x):
self.val = x
self.next = None
def create_linked_list(vals):
root = Node(vals[0])
curr = root
for i in vals[1:]:
temp = Node(i)
curr.next = temp
curr = temp
return root
def find_intersecting_node(a, b):
# Find length of linked list a
len_a = 1
temp = a
while temp.next is not None:
temp = temp.next
len_a += 1
# Find length of linked list b
len_b = 1
temp = b
while temp.next is not None:
temp = temp.next
len_b += 1
# Iterates ptr through longer list so that both linked list ptrs are matched up
for i in range(abs(len_a - len_b)):
if len_a > len_b:
a = a.next
else:
b = b.next
# Iterate both linked lists at the same time until match
while a.val != b.val:
a = a.next
b = b.next
# return value of intersecting node
return a.val
list_a = [int(x) for x in input().split(',')]
list_b = [int(x) for x in input().split(',')]
a = create_linked_list(list_a)
b = create_linked_list(list_b)
print("Intersecting Node: " + str(find_intersecting_node(a, b)))