-
Notifications
You must be signed in to change notification settings - Fork 618
/
linked_lists_intersection.py
178 lines (142 loc) · 5.01 KB
/
linked_lists_intersection.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
'''
Introduction
There are two singly linked lists in a system
Write a program to get the point where two linked list merge.
If they do not merge , print that the given lists do not merge
'''
# Code
# A Linked List Node
class Node:
def __init__(self, data, next=None):
self.data = data
self.next = next
# Utility function to find the total number of nodes in a linked list
def size(head):
# Initialise a int "count" with 0 value
count = 0
# Traverse all nodes till u reach tail
while head:
# Every time u visit a node , increment "count" and move head pointer forward
count += 1
head = head.next
return count
# Utility function to find if given nodes are equal
def eq(head1, head2):
# If both nodes are NONE , they are equal
if(head1==None and head2==None):
return True
# If one of the nodes is NONE , they are not equal
if(None in (head1, head2)):
return False
# If data's of both nodes are equal ,
# return if their successive nodes are equal recursively
if(head1.data == head2.data):
return eq(head1.next, head2.next)
# else return False
return False
# Utility function to find the intersection point of two linked lists.
# Assume that the first linked list is of longer size than second
def findIntersection(first, second, k):
# advance the bigger list by `k` nodes
i = 0
while i < k and first:
first = first.next
i += 1
# simultaneously move both lists at the same speed until they meet
while first and second:
# if both lists meet any node, then that node is the intersection point
if eq(first, second):
return first
# advance both lists by one node
first = first.next
second = second.next
# return None if both lists don't meet
return None
# Function to find the intersection node of two linked lists
def FindIntersectionNode(first, second):
# get the difference in the number of nodes in both lists
diff = size(first) - size(second)
# if the first list has a smaller number of nodes, exchange both lists
if diff < 0:
first, second = second, first
# find and return the intersection node
return findIntersection(first, second, abs(diff))
# Driver Code
if __name__ == '__main__':
# Take input of values seperated by space and
# split them to form a list to store data values of Linked List 1 in order
List1 = [int(value) for value in input("Enter the values of each node of Linked List 1 in order : ").split()]
# Take input of values seperated by space and
# split them to form a list to store data values of Linked List 1 in order
List2 = [int(value) for value in input("Enter the values of each node of Linked List 2 in order : ").split()]
# Bulid Linked List 1 from List 1 values
LinkedList1 = None
temp = LinkedList1
for k in range(len(List1)):
if k == 0:
LinkedList1 = Node(List1[0])
temp = LinkedList1
else:
new = Node(List1[k])
temp.next = new
temp = temp.next
# Build Linked List 2 from List 2 Values
LinkedList2 = None
temp = LinkedList2
for j in range(len(List2)):
if j == 0:
LinkedList2 = Node(List2[0])
temp = LinkedList2
else:
new = Node(List2[j])
temp.next = new
temp = temp.next
# Print Linked List 1
prev = LinkedList1
print("Linked List 1 = ", end = "")
while prev:
print(prev.data, end = "->")
prev=prev.next
print("None")
# Print Linked List 2
prev = LinkedList2
print("Linked List 2 = ", end="")
while prev:
print(prev.data, end = "->")
prev = prev.next
print("None")
# Find Intersection Node
ans = FindIntersectionNode(LinkedList1, LinkedList2)
if ans:
print("The intersection point is", ans.data)
# If node is not NONE , print data of the node
else:
print("The lists do not intersect.")
# If node is NONE , print that the given lists do not intersect
'''
Sample Test Case 1
Input Format :
Example :
Enter the values of each node of Linked List 1 in order : 1 2 3 4 5 6 7
Enter the values of each node of Linked List 2 in order : 5 4 3 4 5 6 7
Output Format :
Example : ( Output to the above input example )
Linked List 1 = 1->2->3->4->5->6->7->None
Linked List 2 = 5->4->3->4->5->6->7->None
The intersection point is 3
Sample Test Case 2
Input Format :
Example :
Enter the values of each node of Linked List 1 in order : 1 2 3 4
Enter the values of each node of Linked List 2 in order : 5 6 7 8
Output Format :
Example : ( Output to the above input example )
Linked List 1 = 1->2->3->4->None
Linked List 2 = 5->6->7->8->None
The lists do not intersect
Time/Space Complexity
Time Complexity : O(m+n)
where m and n are size of two linked lists
Space Complexity : O(1)
No additional space is required
'''