Given the head of two singly linked lists that merge at some point to form a Y-shaped structure. The two lists may have different lengths and contain distinct nodes initially, but from the intersection point onward, they share the same sequence of nodes. Find the node at which the two linked lists first intersect.
[Naive Approach] Using two nested loops - O(m × n) Time and O(1) Space
The idea is to take each node of the first linked list and compare it with every node of the second list. The first common node that appears in both lists will be the intersection point.
Output
15
[Better Approach] Using Hashing - O(m + n) Time and O(m) Space
The idea is to use hashing to store all the nodes of the first list in a hash set and then iterate over second list checking if the node is present in the set. If we find a node which is present in the hash set, we return the node.
Output
15
[Expected Approach - 1] Using difference in node counts - O(m + n) Time and O(1) Space
The two lists share the same tail after the intersection; only their starting parts differ in length. To align them, we should skip the extra nodes in the longer list so both pointers stand at the same distance from the intersection.
Count the nodes in both lists and find the difference (d). Advance the pointer of the longer list by d steps, then move both pointers together until they meet. The meeting point is the intersection.
Output
15
[Expected Approach - 2] Using Two Pointer Technique - O(m + n) Time and O(1) Space
The idea is to traverse the two given linked lists simultaneously, using two pointers. When one pointer reaches the end of its list, it is reassigned to the head of the other list. This process continues until the two pointers meet, which indicates that they have reached the intersection point.
Why this works:
Suppose the lengths of the two lists are m and n, and they share a common tail of length c.
The unique parts before the intersection are m - c and n - c.
By switching lists, each pointer travels (m - c) + c + (n - c) = m + n - c steps before reaching the intersection.
Since both pointers cover exactly m + n - c steps, they must arrive at the intersection at the same time.
Step by Step Approach:
Initialize two pointers ptr1 and ptr2 at head1 and head2 respectively.
Traverse through the lists, one node at a time. => When ptr1 reaches the end of a list, then redirect it to head2. => Similarly, when ptr2 reaches the end of a list, redirect it to the head1. => Once both of them go through reassigning, they will be at equal distance from the collision point. => If at any node ptr1 meets ptr2, then it is the intersection node.
After the second iteration if there is no intersection node, returns NULL.
Output
15
[Expected Approach - 3] Intersection Detection using Floyd’s Cycle-Finding Algorithm
If two linked lists intersect, the intersection point is the place where their paths merge into one. One way to detect this is to convert the problem into a cycle detection problem. If we somehow make the shared portion of the two lists form a loop, then standard cycle detection techniques can be applied to locate the intersection.
To achieve this, we connect the second list to the end of the first list. If the lists intersect, this connection will create a cycle and first node of cycle will be our intersection point.
Time Complexity: O(m + n), where m and n are the lengths of the two linked lists. Running Floyd’s cycle detection take linear time in the size of the lists. Auxiliary Space: O(1)