Union of Two Linked Lists | Practice | GeeksforGeeks

Given two linked lists, your task is to complete the function makeUnion(), that returns the union of two linked lists. This union should include all the distinct elements only.
Example 1:
Input:
L1 = 9->6->4->2->3->8
L2 = 1->2->8->6->2
Output: 1 2 3 4 6 8 9

Your Task:
The task is to complete the function makeUnion() which makes the union of the given two lists and returns the head of the new list.
Note: The new list formed should be in non-decreasing order.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1
0
singhvipnesh65 days ago
class Sol{public static Node findUnion(Node head1,Node head2){    HashSet<Integer>set=new HashSet<>();    Node temp1=head1;    Node temp2=head2;    while(temp1!=null)    {        if(!set.contains(temp1.data))        set.add(temp1.data);        temp1=temp1.next;    }    while(temp2!=null)    {        if(!set.contains(temp2.data))        {            set.add(temp2.data);        }        temp2=temp2.next;    }   ArrayList<Integer>list=new ArrayList<>(set);   Node R=null,dummy=null;   for(int i=0;i<list.size();i++)   {       Node newnode = new Node(list.get(i));        if(R == null){            R = newnode;            dummy = R;   }   else    {       dummy.next=newnode;       dummy=dummy.next;   }   }      return R;}}
0
tthakare735 days ago
//java solution -> TC -> 1.20
//hint -> ArrayList or HashSet

class Sol{
	public static Node findUnion(Node head1,Node head2){
	    ArrayList<Integer> union = new ArrayList<>();
	    
	    while(head1 != null){
	        if(!union.contains(head1.data)) union.add(new Integer(head1.data));
	        head1 = head1.next;
	    }
	    
	    while(head2 != null){
	        if(!union.contains(head2.data)) union.add(new Integer(head2.data));
	        head2 = head2.next;
	    }
	    
	    //sort arrayList
	    Collections.sort(union);
	    
	    Node Result = null, dummy = null;
	    for(int i = 0; i < union.size(); i++){
	        Node newNode = new Node(union.get(i));
	        if(Result == null){
	            Result = newNode;
	            dummy = Result;
	        } else {
	            dummy.next = newNode;
	            dummy = dummy.next;
	        }
	    }
	    
	    return Result;
	}
}
0
mitradiptamoy2 weeks ago
//C++ Soln using SETstruct Node* makeUnion(struct Node* head1, struct Node* head2){   // code here   Node* ans=new Node(-1);   Node* x=ans;   set<int>s;   while(head1!=NULL)   {       s.insert(head1->data);       head1=head1->next;   }   while(head2!=NULL)   {       s.insert(head2->data);       head2=head2->next;   }   for(auto it:s)   {       Node* t=new Node(it);       ans->next=t;       ans=ans->next;   }   x=x->next;   return x;   } 
0
harshpandeyalfa22 weeks ago
(0.12 sec)
struct Node* makeUnion(struct Node* head1, struct Node* head2){   unordered_map<int,int> s;   Node* it=head1;   while(it)   {       s[it->data];       it=it->next;   }   it=head2;   while(it)   {       s[it->data];       it=it->next;   }   vector<int> q;   for(auto i=s.begin() ; i!= s.end() ;i++)   {       q.push_back(i->first);   }   sort(q.begin(), q.end());   Node* h;   int a=0;   h=new Node(q[a]);   it=h;   a++;   while(a!= q.size())   {       Node* i=new Node(q[a]);       it->next=i;       it=i;       a++;          } 
0
tarunlella20003 weeks ago
JAVA
 public static Node findUnion(Node head1,Node head2){    //Add your code here.    Set<Integer> s = new TreeSet<>();        while(head1 != null){        s.add(head1.data);        head1 = head1.next;    }    while(head2 != null){        s.add(head2.data);        head2 = head2.next;    }    Node ans = new Node(0);    Node temp = ans;    Iterator<Integer> i = s.iterator();    while(i.hasNext()){        ans.next = new Node(i.next());        ans = ans.next;    }    return temp.next;        }
0
uday10924 weeks ago
struct Node* makeUnion(struct Node* head1, struct Node* head2)
{
    set <int> s;
    Node * t1 = head1;
    Node * t2 = head2;
    while(t1 -> next != NULL)
    {
        t1 = t1 -> next;
    }
    t1 -> next = t2;
    t1 = head1;
    
    while(t1 != NULL)
    {
        s.insert(t1 -> data);
        t1 = t1 -> next;
    }
    
    Node * tail = NULL;
    Node * head3 = NULL;
    for(int i : s)
    {
        Node * nw = new Node(i);
        if(head3 == NULL)
            head3 = tail = nw;
        else
            tail -> next = nw;
            tail = nw;
    }
    return head3;
}
0
hr061 month ago
void insertAtTail(struct Node* &head,struct Node* &tail,int data)
{
   Node* node = new Node(data);
   
   if(head == NULL)
   {
       head = node;
       tail = head;
       return;
   }
   
   tail->next = node;
   tail = tail->next;
}
struct Node* makeUnion(struct Node* head1, struct Node* head2)
{
   // code here
   set<int> s;
   Node* curr = head1;
   while(curr != NULL)
   {	
   	   // to keep only distinct elements in a sorted way
       s.insert(curr->data);
       curr = curr->next;
   }
   
   curr = head2;
   
   while(curr != NULL)
   {
       s.insert(curr->data);
       curr = curr->next;
   }
   
   Node* newH = NULL;
   Node* newT = newH;
   
   // iterate over the set and add the elements at the tail of newH
  
   // Displaying set elements
   set<int>::iterator itr;
   for (itr = s.begin(); itr != s.end(); itr++) 
   {
       insertAtTail(newH,newT,*itr);
   }
   
   return newH;
}
 
0
abera25831 month ago
//c++,0.3S
struct Node* makeUnion(struct Node* head1, struct Node* head2){   // code here   set<int>s;   struct Node*temp1=head1;   struct Node*temp2=head2;   while(temp1!=NULL){       s.insert(temp1->data);       temp1=temp1->next;   }       while(temp2!=NULL){       s.insert(temp2->data);       temp2=temp2->next;   }Node* newnode = new Node(0);Node*head=NULL;Node*tail=NULL;for(auto value:s){    if(head==NULL){       newnode->data=value;       head=newnode;       tail=newnode;           }    else{         Node* newnode = new Node(0);         newnode->data=value;         tail->next=newnode;         tail=newnode;    }}return head;   }
0
prabhatc78791 month ago
struct Node* makeUnion(struct Node* head1, struct Node* head2)
{
    set<int>s;
    struct Node* t=head1;
    while(t){
        s.insert(t->data);
        t=t->next;
    }
    t=head2;
    while(t){
        s.insert(t->data);
        t=t->next;
    }
    struct Node* dum=new Node(-1);
    t=dum;
    for(auto x:s){
        struct Node* q=new Node(x);
        t->next=q;
        t=t->next;
    }
    return dum->next;
}
0
anshulgupta966262 months ago
SWEET AND SIMPLE 
struct Node* makeUnion(struct Node* head1, struct Node* head2)
{
    // code here
    Node *temp1 = head1;
    Node *temp2 = head2;
    set<int> s1;
    while(temp1 != NULL){
        s1.insert(temp1->data);
        temp1 = temp1->next;
    }
    while(temp2 != NULL){
        s1.insert(temp2->data);
        temp2 = temp2->next;
    }
    Node *new_node = new Node(0);
    Node *head = NULL;
    Node *tail = NULL;
    set<int, greater<int> >::iterator it;
    for(it = s1.begin() ; it != s1.end() ; it++){
        if(head == NULL){
            new_node = new Node(*it);
            head = new_node;
            tail = head;
        }
        else{
            new_node = new Node(*it);
            tail->next = new_node;
            tail = new_node;
        }
    }
    return head;
}

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.