This post is completed by 1 user
|
Add to List |
Find Intersection Point in Two Linked List
Objective: Given Two linked lists, check whether both lists intersect each other, if yes then find the starting node of the intersection.
An intersection point means the end of one linked list is linked with some node in another linked list and it forms a Y shape.
Input: Two Linked List
Output: Intersection Node or point
Example:
Approach:
- Find the length of both the linked lists say : a_len and b_len
- Find the lenDiff = (a_len ~ b_len)
- Traverse the longer linked list by lenDiff
- Now traverse both the lists at the same time
- Check whether nodes are same, if yes then we have found the intersection point
- If we reach the end of the link lists then there is no intersection point.
Trick Solution:
- Take one linked list and join it both ends.
- Nor for the second Linked List, the problem is reduced to "Find a loop in a linked list and find the starting point of the linked list. So see implementation, Click here.
- Starting point will be our intersection point.
Code:
List A : ->1->10->20->30->40->50->60 List B : ->5->15->30->40->50->60 Intersection found at 30
Also Read: