Intersection Point in Y Shapped Linked Lists | Practice | GeeksforGeeks

Given two singly linked lists of size N and M, write a program to get the point where two linked lists intersect each other.
 
Example 1:
Input:
LinkList1 = 3->6->9->common
LinkList2 = 10->common
common = 15->30->NULL
Output: 15
Explanation:


Example 2:
Input: 
Linked List 1 = 4->1->common
Linked List 2 = 5->6->1->common
common = 8->4->5->NULL
Output: 8
Explanation: 

4              5
|              |
1              6
 \             /
  8   -----  1 
   |
   4
   |
  5
  |
  NULL       
Your Task:
You don't need to read input or print anything. The task is to complete the function intersetPoint() which takes the pointer to the head of linklist1(head1) and linklist2(head2) as input parameters and returns data value of a node where two linked lists intersect. If linked list do not merge at any point, then it should return -1.
Challenge : Try to solve the problem without using any extra space.
 
Expected Time Complexity: O(N+M)
Expected Auxiliary Space: O(1)
 
Constraints:
1 ≤ N + M ≤ 2*105
-1000 ≤ value ≤ 1000
 
0
tusharvatsa2in 11 hours
JAVA Solution
 
class Intersect
{
    //Function to find intersection point in Y shaped Linked Lists.
	int intersectPoint(Node headA, Node headB)
	{
        Node temp1 = headA;
        Node temp2 = headB;
        int Acount = 0;
        int Bcount = 0;
        while(temp1 != null){
            Acount++;
            temp1 = temp1.next;
        }
        while(temp2 != null){
            Bcount++;
            temp2 = temp2.next;
        }
        int diff = Math.abs(Acount - Bcount);
        if(Acount > Bcount){
            temp1 = headA;
            temp2 = headB;
            while(diff != 0){
                temp1 = temp1.next;
                diff--;
            }
        }else{
            temp1 = headA;
            temp2 = headB;
            while(diff != 0){
                temp2 = temp2.next;
                diff--;
            }
        }
        while(temp1 != null && temp2 != null){
            if(temp1 == temp2){
                return temp1.data;
            }
            temp1 = temp1.next;
            temp2 = temp2.next;
        }
        return -1;
	}
}

0
tusharvatsa2
This comment was deleted.
0
pritamhazra21in 5 hours
Java HashSet

	int intersectPoint(Node head1, Node head2)
	{
         HashSet<Node> set = new HashSet<>();
         
         while(head1 != null){
             set.add(head1);
             head1 = head1.next;
         }
         
         while(head2!=null){
             if(set.contains(head2)){
                 return head2.data;
             }
             head2 = head2.next;
         }
         
         return -1;
	}
+1
yuvrajranabtcse205 days ago
{//c++ code  easy soln   Node *go=head1;   while(go->next!=NULL)go=go->next;   go->next=head2;   go=head1;   while(go->next!=NULL){       if(go->data>1000) return (go->data-10000);        go->data+=10000;       go=go->next;}   return -1;}
+1
2019012561 week ago
// Optimized cpp code
int intersectPoint(Node* head1, Node* head2){   int l1 = 0;   int l2=0;   Node* temp1 = head1;   Node* temp2 = head2;   while(temp1!=NULL){       l1++;       temp1=temp1->next;   }   while(temp2!=NULL){       l2++;       temp2=temp2->next;   }   int d = 0;   if(l1 > l2 ){       d = l1-l2;       temp1 = head1;       temp2 = head2;          }   else if (l1<l2){       d = l2-l1;       temp1 = head2;       temp2 = head1;   }   else{       d = 0;       temp1 = head1;       temp2 = head2;   }       for(int i=0;i<d;i++){           temp1=temp1->next;       }       while(temp1!=temp2){           temp1=temp1->next;           temp2=temp2->next;       }       return temp1->data;}
0
2019sushilkumarkori1 week ago

// } Driver Code Ends
//Function to find intersection point in Y shaped Linked Lists.
int intersectPoint(Node* head1, Node* head2)
{
    // Your Code 
    int n1=0,n2=0;
    Node* temp1 = head1;
    Node* temp2 = head2;
    while(temp1!=0){
        n1++;
        temp1=temp1->next;
    }
    while(temp2!=0){
        n2++;
        temp2=temp2->next;
    }
    temp1 = head1;
    temp2 = head2;
    if(n1>=n2){
        int i=0;
        while(temp1!=NULL){
            if(temp1==temp2){
                return temp1->data;
            }
            i++;
            temp1=temp1->next;
            if(i>n1-n2){
                temp2 = temp2->next;
            }
        }
    }
    else{
        int i=0;
        while(temp2!=NULL){
            if(temp1==temp2){
                return temp2->data;
            }
            i++;
            temp2=temp2->next;
            if(i>n2-n1){
                temp1=temp1->next;
            }
        }
    }
    return -1;
}


0
swapniltayal4222 weeks ago
int length(Node *head){   int l=0;   Node* temp = head;   while(temp != NULL){       l++;       temp = temp->next;   }return l;}
int intersectPoint(Node* head1, Node* head2){   int l1 = length(head1);   int l2 = length(head2);
   int d = 0;   Node* ptr1;   Node* ptr2;
   if (l1 > l2){       d = l1 - l2;       ptr1 = head1;       ptr2 = head2;   }else{       d = l2 - l1;       ptr1 = head2;       ptr2 = head1;   }
   while(d){       ptr1 = ptr1->next;       if (ptr1 == NULL){           return -1;       }       d--;   }
   while (ptr1 != NULL && ptr2 != NULL){       if (ptr1 == ptr2){           return ptr1->data;       }       ptr1 = ptr1->next;       ptr2 = ptr2->next;   }
   return -1;}
0
09himanshusah2 weeks ago
JavaScript Solution
class Solution {
    //Function to find intersection point in Y shaped Linked Lists.
    intersectPoint(head1, head2)
    {
        //your code here
        let a = head1;
        let b = head2;
        while(a != b) {
            if(a == null) a = head2;
            else a = a.next;
            
            if(b == null) b = head1;
            else b = b.next;
        }
        return a.data;
    }
}
+5
shreyassakariya20022 weeks ago
int intersectPoint(Node* head1, Node* head2){   Node *p1=head1,*p2=head2;      while(1)   {       if(p1==p2)           return p1->data;       p1=p1->next;       p2=p2->next;              if(p1==NULL and p2==NULL)           return -1;                  if(p1==NULL)           p1=head2;       if(p2==NULL)           p2=head1;   }   return -1;}
+2
wolfofsv2 weeks ago
find len1 and len2
skip abs(len1 - len2) in bigger LL
traverse the two LLs till u find intersection if there is any
O(n) time and O(1) space
int FindLength(Node* head){
    int len = 0;
    while(head != NULL){
        head = head -> next;
        len++;
    }
    return len;
}
Node* SkipLL(Node* head, int len){
    for(int i = 0; i < len; i++){
        head = head -> next;
    }
    return head;
}


int intersectPoint(Node* head1, Node* head2)
{
    // Your Code Here
    int len1 = FindLength(head1), len2 = FindLength(head2);
    if(len1 > len2){
        head1 = SkipLL(head1, len1 - len2);
    }
    else{
        head2 = SkipLL(head2, len2 - len1);
    }
    while(head1 != head2 && head1 != NULL){
        head1 = head1 -> next;
        head2 = head2 -> next;
    }
    if(head1 != NULL){
        return head1 -> data;
    }
    return -1;
    
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.