Find Intersection Point in Two Linked List

Objective: Given Two linked list, check whether both list intersect each other, if yes then find the starting node of the intersection.

Intersection point means 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:

Find Intersection Point in Two Linked List
Find Intersection Point in Two Linked List

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:

 Time Complexity : O(n) , Space Complexity : O(1)
Complete Code:

//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.
public class FindIntersectionOfLinkedLists {
public LinkedListIntersection a;
public LinkedListIntersection b;
public void createLists(){
a = new LinkedListIntersection();
a.addAtEnd(1);
a.addAtEnd(10);
a.addAtEnd(20);
Node tmp = a.addAtEnd(30);
a.addAtEnd(40);
a.addAtEnd(50);
a.addAtEnd(60);
System.out.print("List A : ");
a.display();
b = new LinkedListIntersection();
b.addAtEnd(5);
b.addAtEnd(15);
b.createIntersection(a,tmp);
System.out.print("List B : ");
b.display();
}
public void findIntersectionByLength(){
int a_len=0;
int b_len=0;
int lenDiff=0;
boolean intsctFound = false;
Node an = a.head;
Node bn = b.head;
while(an!=null){
an=an.next;
a_len++;
}
while(bn!=null){
bn=bn.next;
b_len++;
}
an = a.head;
bn = b.head;
if(a_len>b_len){
lenDiff = a_lenb_len;
// System.out.print("length diff " +lenDiff );
while(lenDiff!=0){
an = an.next;
lenDiff;
}
}else{
lenDiff = b_lena_len;
while(lenDiff!=0){
bn = bn.next;
lenDiff;
}
}
while(an!=null && bn!=null){
//System.out.print(an.data + " " + bn.data);
if(an==bn) {
System.out.print("Intersection found at " + an.data);
intsctFound = true;
break;
}
else{
an = an.next;
bn = bn.next;
}
}
if(intsctFound!=true){
System.out.print("Intersection Not Found");
}
}
public static void main (String[] args) throws java.lang.Exception
{
FindIntersectionOfLinkedLists i = new FindIntersectionOfLinkedLists();
i.createLists();
i.findIntersectionByLength();
}
}
class Node{
public int data;
public Node next;
public Node(int data){
this.data = data;
this.next = null;
}
}
class LinkedListIntersection{
public Node head;
public LinkedListIntersection(){
head=null;
}
public Node addAtEnd(int data){
Node n = new Node(data);
if (head==null){
n.next = head;
head = n;
}
else{
Node currNode = head;
while(currNode.next!=null){
//System.out.print("—->" + currNode.data);
currNode = currNode.next;
}
currNode.next = n;
}
return n;
}
public void createIntersection(LinkedListIntersection a, Node nd){
Node hd = a.head; // this is the list to whcih another list will intersect, in our example its list a
while(hd!=nd){
hd = hd.next;
}
Node currNode = head;// this is for the list which will connect, in our example its list b
while(currNode.next!=null){
currNode = currNode.next;
}
currNode.next = hd; ;
}
public void display(){
System.out.println("");
Node currNode = head;
while(currNode!=null){
System.out.print("->" + currNode.data);
currNode=currNode.next;
}
System.out.println("");
}
}

Output:

List A :
->1->10->20->30->40->50->60
List B :
->5->15->30->40->50->60
Intersection found at 30