Intersection of two sorted Linked lists | Practice | GeeksforGeeks

Given two lists sorted in increasing order, create a new list representing the intersection of the two lists. The new list should be made with its own memory — the original lists should not be changed.
Note: The list elements are not necessarily distinct.
Example 1:
Input:
L1 = 1->2->3->4->6
L2 = 2->4->6->8
Output: 2 4 6
Explanation: For the given first two
linked list, 2, 4 and 6 are the elements
in the intersection.
Example 2:
Input:
L1 = 10->20->40->50
L2 = 15->40
Output: 40

Your Task:
The task is to complete the function intersection() which should find the intersection of two linked list and add all the elements in intersection to the third linked list and return the head of the third linked list.
Expected Time Complexity : O(n+m)
Expected Auxilliary Space : O(n+m)
Note: n,m are the size of the linked lists.
Constraints:
1 <= size of linked lists <= 5000
1 <= Data in linked list nodes <= 1000
0
bhavinraichura28in 7 hours
C++ solution
 
Node* findIntersection(Node* head1, Node* head2){   // Your Code Here   Node *temp,*l1,*l2,*l3;   l3=new Node(0);   l1 = head1;   l2=head2;   temp=l3;      while(l1!=NULL && l2!=NULL){       if (l1->data == l2->data){           temp->next=new Node(l1->data);           temp=temp->next;           l2= l2->next;           l1= l1->next;       }       else if(l1->data > l2->data)           l2= l2->next;       else            l1=l1->next;   }      return l3->next;}
0
deeptimayeemaharana06
This comment was deleted.
+2
shahabuddinbravo404 days ago
Node* findIntersection(Node* head1, Node* head2){    Node *ptr1=head1,*ptr2=head2;        unordered_set<int>s;        while(ptr2){        s.insert(ptr2->data);        ptr2=ptr2->next;    }        Node *list= new Node(-1),*head=list;        while(ptr1)    {       //  auto it=s.find(ptr1->data);       //  if(it!=s.end()){       //      list->next=ptr1;       //      list=list->next;       //  }       //  ptr1=ptr1->next;              auto it=s.find(ptr1->data);       if(it!=s.end()){           Node *temp=new Node(ptr1->data);           list->next=temp;           list=list->next;       }       ptr1=ptr1->next;    }    //list->next=NULL;        head=head->next;        return head;}
+1
2019sushilkumarkori4 days ago

Node* findIntersection(Node* head1, Node* head2)
{
    Node* temp1 = head1;
    Node* temp2 = head2;
    Node* new_head = new Node(0);
    Node* temp3 = new_head;
    while(temp1!=NULL && temp2!=NULL){
        if(temp1->data > temp2->data){
            temp2=temp2->next;
        }
        else if(temp1->data < temp2->data){
            temp1=temp1->next;
        }
        
        else{
            temp3->next=new Node(temp1->data);
            temp3=temp3->next;
            temp1=temp1->next;
            temp2=temp2->next;
        }
     
        }
        temp3->next = NULL;
        return new_head->next;
    }
    

0
sy9924554 days ago
 HashSet<Integer> set = new HashSet<>();
       while(head2 != null){
           set.add(head2.data);
           head2 = head2.next;
       }
       Node dummy = new Node(-1);
       Node itr = dummy;
       while(head1 != null){
           if(set.contains(head1.data)){
               itr.next = new Node(head1.data);
               itr = itr.next;
           }
           head1 = head1.next;
       }
       return dummy.next;
+1
mohitraj27414 days ago
Note: Vector c stores common elements.
Node* findIntersection(Node* head1, Node* head2){   // Your Code Here   vector<int>c;  Node* temp1=head1;  Node* temp2=head2;  while(temp1!=NULL && temp2!=NULL)  {      if(temp1->data < temp2->data)      temp1=temp1->next;      else if(temp1->data > temp2->data)      temp2=temp2->next;      else      {          c.push_back(temp1->data);          temp1=temp1->next;          temp2=temp2->next;                }  }  Node* t = new Node(0);  Node* h=t;   int k=0;   while(k!=c.size())   {       h->next=new Node(c[k]);       h=h->next;       k++;   }   h->next=NULL;   return t->next;   }
0
codewithaddy6 days ago
C++ Solution
Time Taken: 0.08 sec
Node* findIntersection(Node* head1, Node* head2)
{
    Node* t1=head1;
    Node* t2=head2;
    
    vector<int> v;
    while(t1 && t2){
        if(t1->data==t2->data){
            v.push_back(t1->data);
            t1=t1->next;
            t2=t2->next;
        }
        else if(t1->data<t2->data){
            t1=t1->next;
        }
        else{
            t2=t2->next;
        }
    }
    
    Node* q=new Node(v[0]);
    Node* it=q;
    for(int i=1; i<v.size();i++){
        it->next=new Node(v[i]);
        it=it->next;
    }
    return q;
}
0
09himanshusah6 days ago
JavaScript Solution
class Solution {
    
    findIntersection(head1, head2)
    {
        //your code here
        let dummy = new Node(-1);
        let tail = dummy;
        let l1 = head1;
        let l2 = head2;
        while(l1 !== null && l1 !== null) {
            if(l1.data == l2.data) {
                tail.next = l1;
                tail = l1;
                l1 = l1.next;
                l2 = l2.next;
            } else if(l1.data < l2.data) {
                l1= l1.next;
            } else {
                l2 = l2.next;
            }
        }
        return dummy.next;
    }
}

0
gamerextension0072 weeks ago
Update your solution to fulfill the disclaimer note
5 4
1 2 2 4 62 2 6 8
Your solution would output wrong for this
0
tthakare732 weeks ago
//Java Solution TC -> 1.17
//hint -> Two pointer Approch
class Sol{
   public static Node findIntersection(Node head1, Node head2){
        Node Result = null, dummy = null;
        while(head1 != null && head2 != null){
            if(head1.data == head2.data){
                Node NewNode = new Node(head1.data);
                
                if(Result == null){
                    Result = NewNode;
                    dummy = NewNode;
                } else {
                    dummy.next = NewNode;
                    dummy = dummy.next;
                }
                
                head1 = head1.next;
                head2 = head2.next;
            } else {
                if(head1.data < head2.data) head1 = head1.next;
                else head2 = head2.next;
            }
        }
        return Result;
   }
}
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.