Intersection of Two Linked Lists | Practice | GeeksforGeeks

Given two linked lists, the task is to complete the function findIntersection(), that returns the intersection of two linked lists. Each of the two linked list contains distinct node values.
Example 1:
Input:
LinkedList1: 9->6->4->2->3->8
LinkedList2: 1->2->8->6
Output: 6 2 8

Your Task:
You task is to complete the function findIntersection() which takes the heads of the 2 input linked lists as parameter and returns the head of intersection list. Returned list will be automatically printed by driver code.
Note: The order of nodes in this list should be same as the order in which those particular nodes appear in input list 1.
Constraints:
1 <= n,m <= 104
Expected time complexity: O(m+n)
Expected auxiliary space: O(m+n)
+1
shahabuddinbravo405 days ago
class Solution{ public:   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;       }       list->next=NULL;              head=head->next;              return head;   }};
0
singhvipnesh66 days ago
class Solution{   public static Node findIntersection(Node head1, Node head2)   {      HashSet<Integer>set=new HashSet<>();      ArrayList<Integer>list=new ArrayList<>();      Node temp1=head1;      Node temp2=head2;      while(temp2!=null)      {          if(!set.contains(temp2.data))          set.add(temp2.data);          temp2=temp2.next;      }      while(temp1!=null)      {          if(set.contains(temp1.data))          list.add(temp1.data);          temp1=temp1.next;      }      Node main=null,dummy=null;      for(int i=0;i<list.size();i++)      {            Node newNode = new Node(list.get(i));              if(main==null)            {                main=newNode;                dummy=main;            }            else            {                dummy.next=newNode;                dummy=dummy.next;            }                  }      return main;   }}
0
tthakare731 week ago
//java Solution
class Solution{
    public static Node findIntersection(Node head1, Node head2){
        HashSet<Integer> set = new HashSet<>();
        while(head2 != null){
            set.add(head2.data);
            head2 = head2.next;
        }
        Node result = null, dummy = null;
        while(head1 != null){
            if(set.contains(head1.data)){
                if(result == null) {
                    result = new Node(head1.data);
                    dummy = result;
                } else {
                    dummy.next = new Node(head1.data);
                    dummy = dummy.next;
                }
            }
            head1 = head1.next;
        }
        return result;
    }
}
0
prabhakarsati294261 week ago
class Solution:   def findIntersection(self, head1, head2):       # code here       # return head of intersection list       cur1 = head2       cur2 = head1       l = linkedList()       s = set()       while cur1:           s.add(cur1.data)           cur1 = cur1.next       while cur2:           if cur2.data in s:               l.insert(cur2.data)           cur2 = cur2.next       return l.head
0
hharshit81183 weeks ago
 Node* findIntersection(Node* a, Node* b)   {       if(a == NULL || b== NULL)       return NULL;       Node *res = NULL, *ptr = NULL;       unordered_set<int> hs;       while(b){           hs.insert(b->data);           b = b->next;       }       while(a){           if(hs.find(a->data) != hs.end()){               Node* temp = new Node(a->data);               if(res == NULL){                   res = temp;                   ptr = res;               }               else{                   res->next = temp;                   res = res->next;               }           }           a = a->next;       }       return ptr;   }
0
harshpandeyalfa23 weeks ago
C++
(0.44 sec)
Node* findIntersection(Node* head1, Node* head2)   {     queue<int> q;     Node* h;     unordered_set<int> us;     Node* it=head2;     while(it)     {       us.insert(it->data);       it=it->next;     }     it=head1;     while(it)     {         if(us.find(it->data) != us.end())         {             q.push(it->data);         }         it=it->next;     }     h=new Node(q.front());     it=h;     q.pop();     while(!q.empty())     {         Node*i=new Node(q.front());         it->next=i;         it=i;         q.pop();     }     return h;   }
0
mitradiptamoy3 weeks ago
//Intersection of Two Linked Lists 
 
Node* findIntersection(Node* head1, Node* head2)   {       // code here       // return the head of intersection list      Node* p1=head1;      Node* p2=head2;      Node* curr=new Node(0);      Node* head=curr;      unordered_set<int>s;      while(p2!=NULL)      {          s.insert(p2->data);          p2=p2->next;      }      while(p1!=NULL)      {          if(s.find(p1->data)!=s.end())          {              Node* t=new Node(p1->data);              curr->next=t;              curr=curr->next;          }          p1=p1->next;      }      head=head->next;      return head;   }
0
as0042301 month ago
In C++;
    Node* findIntersection(Node* head1, Node* head2)   {       Node *intsct = new Node(-1);       Node *cur3 = intsct;       Node *cur1 = head1;       Node *cur2 = head2;       unordered_map<int, int> mp;              while(cur2)       {           mp[cur2->data] = 1;           cur2 = cur2->next;       }       while(cur1)       {           if(mp[cur1->data]){               Node *temp = new Node(cur1->data);               cur3->next = temp;               cur3 = temp;           }           cur1 = cur1->next;       }       return intsct->next;   }
0
gupta2411sumit1 month ago
// Easy C++ Solution
Node* findIntersection(Node* head1, Node* head2)   {       // code here       // return the head of intersection list       Node *p = head2 ;       Node *q = head1 ;       Node *ans = new Node(0) ;       Node *head = ans ;              set<int>s ;              while(p!=NULL)       {           s.insert(p->data) ;           p = p->next ;       }              while(q!=NULL)       {           auto it = s.find(q->data) ;           if(it != s.end())           {               Node *temp = new Node(q->data) ;               ans->next = temp ;               ans = temp ;           }                      q = q->next ;       }              return head->next ;   }
+1
mashhadihossain1 month ago
SIMPLE JAVA SOLUTION (2.2 /3.6 SEC)
class Solution{   public static Node findIntersection(Node head1, Node head2)   {       Node temp1=head1;       Node temp2=head2;              Node dummy=new Node(-1);       Node curr=dummy;       HashSet<Integer> set=new HashSet<Integer>();       while(temp2!=null)       {           set.add(temp2.data);           temp2=temp2.next;       }       while(temp1!=null)       {           if(set.contains(temp1.data))           {               Node extra=new Node(temp1.data);               dummy.next=extra;               dummy=dummy.next;           }           temp1=temp1.next;       }       return curr.next;   }}
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.