Finding the Intersection Node of Two Linked ListsIn this problem, we will be given two linked lists. The linked lists will be merged at a node of both the linked lists, forming a Y-shaped list. We have to find the node at which the linked lists merge. Let us see some examples to understand the question Input: List1 = 1 -> 2 -> 3 -> 4 -> 5 List2 = 9 -> 8 -> 4 -> 5 Output: 4 The two lists merge after node 4 of both linked lists Method - 1The most basic approach to solving this problem is to use a nested loop. The outer loop will traverse through every node of the first list, and the inner while loop will traverse through the second loop. The inner while loop will check if the current node of list 2 is the same as the current node of list 1. If the condition is satisfied, then we have found the intersection node. If the loop ends without finding the intersection node, then we will return None. Below is the Python program for the implementation of the above approach. Code Output: The intersection node is: 4 Time Complexity: Due to the nested loop, the time complexity of this program will be O(m * n). Here, m and n are the number of nodes in the two linked lists. Space Complexity: We are not using any extra space; hence, the space complexity is constant. Method - 2In this approach, we will use a different structure of the linked list. We will make some modifications to the basic structure, which will help us to solve this problem. We will add a new variable to the linked list, which will store the Boolean value True or False. This variable will keep track of whether a node is already visited or not. We will start by traversing the first linked list and mark all the nodes of the first linked list as visited. Then, we will traverse the second linked list. For each node, we will check if the node is already visited or not. If the node is already visited, that means we have visited that node in the first linked list, which implies that the linked lists intersect at this particular node. If we have completed the traversal of the second node without reaching a node that is already visited, that means the two linked lists do not intersect at any point. This approach is better than the previous approach as we are not using nested loops to traverse the linked lists. We will use linear traversal to find the intersection of the nodes. Code Output: The intersection node is: 4 We do not need to create a new variable. We can use hash table too. Code Output: The intersection node is: 4 Time Complexity: We are traversing both lists once. To traverse the lists, we have used a linear loop. Therefore, the time complexity of traversing will be the sum of the lengths of both the linked lists. Hence, the time complexity will be O(m + n). m and n are the lengths of the two linked lists. We are taking total lengths because, in the worst-case scenario, the linked lists will not have any intersection, and in that case, complete linked lists will be traversed. Auxiliary space: We have created a list to store the nodes that are already visited. The nodes will be stored only once. Therefore, the space complexity will be the length of this list. In the worst-case scenario, the length of this list will be O( max(m, n) ), where max(m, n) is the length of the longer linked lists out of the two. Method - 3In this approach, we will make use of the difference in the count of the two linked lists to find the intersection node of the two linked lists. We will follow these steps to solve this problem.
Below is the Python code for this approach. Code Output: The intersection node is: 4 Time Complexity: O(m + n) Auxiliary Space: O(1) Method - 4In this method, we will use hashing to find the intersection node. This method is similar to the method when we used a list to store the nodes that we have visited, except that this time, we will use a HashSet to store the nodes. The algorithm is simple. First, we will initialize an empty hash set. After initializing, we will traverse the first linked list and store all the nodes of this linked list in the hash set. In the next step, we will traverse the second linked list, and for each node, we will check if that node is in the hash set or not. If the node is in the hash set, that means it is the node of the intersection, and hence, we will return that node. If there is no node of intersection, the function will return False. Code Output: The intersection node is: 4 Time complexity: We are using two linear loops which will iterate till both of the linked lists are traversed; therefore the time complexity of the program is O( m + n ) Auxiliary Space: We have used extra space to store the nodes; therefore, the space complexity is linear, i.e., O(n). Method - 5In this method, we will use the famous two-pointer approach to find the intersection node. Here is the algorithm of this approach:
Code Output: The intersection node is: 4 Time complexity: We are using a linear loop which will iterate till both of the linked lists are traversed; therefore the time complexity of the program is O( m + n ) Auxiliary Space: We have not created any extra space to store something; therefore, the space complexity is constant, i.e., O(1). Method - 6In this approach, we will use two stacks to find the intersection node. Firstly, we will initialize two stacks. In the next step, we will traverse both the linked lists and add all of the nodes of both of the linked lists to each of the stacks. Then, we will initialize a variable which will store the intersection node. Let this variable be merge_point = None. We will start a while loop, which will continue until the top elements of the stack are not equal. Inside the while loop, we will pop the top elements of the stack and store that node in the variable merge_point. When the while loop ends, we will return nerge_point, which will have the intersection node of the two linked lists. If there is no intersection point of the linked lists, then the while loop will not be executed, and merge_point = None will be returned. Code Output: The intersection node is: 4 Time Complexity: We are traversing both lists once. To traverse the lists, we have used a linear loop. Therefore, the time complexity of traversing will be the sum of the lengths of both the linked lists. Hence, the time complexity will be O(m + n). m and n are the lengths of the two linked lists. The while loop will take O(min(m, n)) in the worst-case scenario. Therefore, the average time complexity is O(m, n). Auxiliary Space: We have created two stacks to store the nodes of both of the linked lists. The nodes will be stored only once. Therefore, the space complexity will be the total length of two stacks. Hence, the space complexity will be O(m + n). |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India