# Finding the Intersection Node of Two Linked Lists

In 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 - 1

The 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 - 2

In 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 - 3

In 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.

• Firstly, we will count the number of nodes present in the first linked list.
• Then, we will find the count of the number of nodes present in the second linked list.
• We will then find the difference between the number of nodes of the two linked lists. Let this difference be stored as diff.
• Now, we have to traverse the linked lists with the greater number of nodes. We will traverse this list starting from the first node to the dth node of the list. From the dth node forward, both the linked lists have the same number of nodes.
• Now, we will run another loop for traversing both of the linked lists together. We will traverse the lists parallel and check if there is any common node. The first common node of the two lists will be the node of intersection.
• If we do not find any intersection node, we will return False.

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 - 4

In 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 - 5

In this method, we will use the famous two-pointer approach to find the intersection node. Here is the algorithm of this approach:

• Firstly, we will initialize two pointers, which will point towards the head of the two linked lists. Let these pointers be p1 and p2, pointing towards list1 and list2.
• Now, we will initialize a while loop. This while loop will continue until the point when the two pointers point towards the same node.
• In the while loop, we will move both of the pointers towards the next node of their respective linked lists.
• In the next step, we will check if both of the pointers point towards None. If this is the case, then the linked lists have no intersection node. Hence, we will return None.
• If any one of the pointer points towards None, then we will redirect it towards the head node of its linked list. That is, if p1 == None then p1 = list1 and if p2 == None then p2 = list2.
• After some iterations, either both of the pointers will be pointing towards None or the intersection node. Hence, the while loop will be terminated.

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 - 6

In 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).