-
Notifications
You must be signed in to change notification settings - Fork 3
/
20_Intersecting_point_of_two_linkend_lists.py
executable file
·87 lines (59 loc) · 1.97 KB
/
20_Intersecting_point_of_two_linkend_lists.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
80
81
82
83
84
85
86
'''
This problem was asked by Google.
Given two singly linked lists that intersect at some point, find the intersecting node. The lists are non-cyclical.
For example, given A = 3 -> 7 -> 8 -> 10 and B = 99 -> 1 -> 8 -> 10, return the node with value 8.
In this example, assume nodes with the same value are the exact same node objects.
Do this in O(M + N) time (where M and N are the lengths of the lists) and constant space.
'''
"""
Assuming some thing like this
list1 = 1->2->3->7->8->10
list2 = 99->1->8->10
so visualizing:
1
2
3
7
99->1->8->10
"""
class LinkedList:
def __init__(self, data, next=None):
self.data = data
self.next = next
def print_list(list):
if list is None:
return
print(list.data)
print_list(list.next)
def find_intersection(list1, list2):# O(M*K)
pointerL2 = list2
while pointerL2:
pointerL1 = list1
while pointerL1:
if pointerL1 == pointerL2: # found intersection
return pointerL1.data
pointerL1 = pointerL1.next
pointerL2 = pointerL2.next
def find_intersection_2(list1, list2):# O(M+K)
# idea store pointer values to a dictionary
# return when match found
dict_visited = {} # dict of visited nodes
pointer = list1
while pointer:
dict_visited[id(pointer)] = None # just add the key no need to store a value
pointer = pointer.next
pointer = list2
while pointer:
if id(pointer) in dict_visited:
return ('intersection point at: {}'.format(pointer.data))
pointer = pointer.next
return 'No Intersection'
if __name__ == '__main__':
common_tail = LinkedList(8, next=LinkedList(10))
l1 = LinkedList(1, next=LinkedList(2,next=LinkedList(3, next=LinkedList(7, next=common_tail))))
l2 = LinkedList(99, next=LinkedList(1, next=common_tail))
# print_list(l1)
# print('\n')
# print_list(l2)
# print('\n')
print(find_intersection_2(l1,l2))