Given two linked lists, where the tail of the second list points to a node in the first list, find the node where both lists intersect.

Consider the following linked lists where the second list’s tail is connected to the first list’s fourth node. The solution should return a pointer to the node 4 as the intersection point.

Intersection of two Linked List

A simple solution is to consider each node of the first list and check if it can be reached from the second list. The first node in the first list that is reachable from the second list is the intersection point. The time complexity of this solution is O(N×M) where N is the length of the first list and M is the length of the second list.
However, the solution is far from optimal. This post provides an overview of several alternatives to improve the time complexity.

The idea is to traverse the first list and store each node’s address in a hash table. Then traverse the second list and get the address of the first node present in the hash table. This node would be the intersection point. Below is C++, Java, and Python implementation based on the above idea –

C++

Download   Run Code

Output:

The intersection point is 4

Java

Download   Run Code

Output:

The intersection point is 4

Python

Download   Run Code

Output:

The intersection point is 4

Download   Run Code
Output:

The intersection point is 4

Download   Run Code
Output:

The intersection point is 4

Download   Run Code
Output:

The intersection point is 4

The time complexity of the above solution is O(N + M) where N is the length of the first list and M is the length of the second list. However, the program uses O(N) additional space for storing nodes of the first list in a hash table.

Another approach is to make the first linked list circular by linking its tail to the head. Then, the problem reduces to finding a loop in the second linked list.
The idea is to get a pointer to the loop node using Floyd’s Cycle detection algorithm and count the number of nodes in the loop using that loop node (say k). Then take two pointers – one pointing to the head node and another pointing to the k'th node from the head. If we simultaneously move these pointers at the same speed, they will eventually meet at the loop’s starting node.
The algorithm can be implemented as follows in C++, Java, and Python:

C++

Download   Run Code

Output:

The intersection point is 4

Java

Download   Run Code

Output:

The intersection point is 4

Python

Download   Run Code

Output:

The intersection point is 4

Download   Run Code
Output:

The intersection point is 4

Download   Run Code
Output:

The intersection point is 4

Download   Run Code
Output:

The intersection point is 4

The time complexity of the above solution is O(N + M) where N is the length of the first list and M is the length of the second list.

We can also use the difference in node counts of both lists to find the intersection point. The idea is to advance the bigger list by k nodes, where k is the difference in the number of nodes in both lists. Then, move both lists at the same speed until they intersect with each other. The node at which both lists intersect is the intersection point.
This logic can be implemented as follows in C++, Java, and Python:

C++

Download   Run Code

Output:

The intersection point is 4

Java

Download   Run Code

Output:

The intersection point is 4

Python

Download   Run Code

Output:

The intersection point is 4

Download   Run Code
Output:

The intersection point is 4

Download   Run Code
Output:

The intersection point is 4

Download   Run Code
Output:

The intersection point is 4

The time complexity of the above solution is O(N + M) where N is the length of the first list and M is the length of the second list.

Observation: The number of nodes in the first list + distance of the head of the second list from the intersection point = The number of nodes in the second list + distance of the head of the first list from the intersection point.
The idea is to take two pointers, x and y, initially pointing to the head of the first list and the second list’s head, respectively. Then, advance both pointers at the same pace until they meet at a common node. When x reaches its end, redirect it to the head of the second list. When y reaches its end, turn it to the head of the first list. The node where x meets y is the intersection node.
The algorithm can be implemented as follows in C++, Java, and Python:

C++

Download   Run Code

Output:

The intersection point is 4

Java

Download   Run Code

Output:

The intersection point is 4

Python

Download   Run Code

Output:

The intersection point is 4

Download   Run Code
Output:

The intersection point is 4

Download   Run Code
Output:

The intersection point is 4

Download   Run Code
Output:

The intersection point is 4

The time complexity of the above solution is O(N + M) where N is the length of the first list and M is the length of the second list.

source