Remove all occurences of duplicates in a linked list | Practice | GeeksforGeeks

Given a sorted linked list, delete all nodes that have duplicate numbers (all occurrences), leaving only numbers that appear once in the original list. 
Example 1:
Input: 
N = 8
Linked List = 23->28->28->35->49->49->53->53
Output: 
23 35
Explanation:
The duplicate numbers are 28, 49 and 53 which 
are removed from the list.
Example 2:
Input:
N = 6
Linked List = 11->11->11->11->75->75
Output: 
Empty list
Explanation:
All the nodes in the linked list have 
duplicates. Hence the resultant list 
would be empty.
 
0
harshkumar12sept1 week ago
// iterative solution→   
Node* removeAllDuplicates(struct Node* head)   {     Node* temp = head;     Node* temp2 =NULL; // this is the variable to check if we have to delete the current temp;
     Node* prev = NULL;          while(temp->next!=NULL){                  if(temp->data == temp->next->data){             Node* toDel = temp->next;             temp->next = temp->next->next;             delete toDel;             temp2 = temp;         }else{                          if(temp2 == temp){                 if(temp==head){  // if the element to be deleted is the head;                     head = temp->next;                     delete temp;                     temp = head;                 }else{                        Node* toDel1 = temp;                        prev->next = temp->next;                        temp = prev->next;                        delete toDel1;                 }                         }else{                 prev = temp;                 temp = temp->next;             }                      }     }     if(temp2 == temp){         if(!prev){             return NULL;         }else{           Node* todel3 = temp;           temp = prev;           temp->next = NULL;           delete todel3;         }     }     return head;   }   
0
harshilgupta00991 week ago
 Node* removeAllDuplicates(struct Node* head)
    {
        //code here
        if(head==NULL or head->next==NULL){
            return head;
        }
        Node* start=new Node(0);
        
        start->next=head;
        
        Node* prev=start;
        while(head!=NULL){
            if(head->next && head->data==head->next->data){
                while(head->next && head->data==head->next->data)
                    head=head->next;
            
                prev->next=head->next;
            }
            else{
                prev=prev->next;
            }
            head=head->next;
        }
        return start->next;
    }
0
aliaksandrhn1 month ago
class Solution
{
    public static Node removeAllDuplicates(Node head)
    {
        Node dummy = new Node(-1), cur = dummy;
        dummy.next = head;
        while(cur != null && cur.next != null) {
            Node next = cur.next, run = next;
            while(run != null && run.data == next.data)
                run = run.next;
            if(next.next == run) 
                cur = cur.next;
            else 
                cur.next = run;
        }
        return dummy.next;
    }
}
+1
emmanueluluabuike1 month ago
Simple Java solution with explanation
Logic is to count the duplicates using a HashMap, then add no occurring numbers into a newNode
After this process you then sort this newNode (using insertion sort logic) and then return
Map<Integer, Integer> map = new HashMap<>();
        Node curr = head;
        Node newNode = new Node(0);
        Node dummy = newNode;
        
        if (curr == null)
            return null;
        
        while (curr != null){
            if(map.containsKey(curr.data))
                map.put(curr.data, map.get(curr.data) + 1);
            else 
                map.put(curr.data, 1);
            curr = curr.next;
        }
        
        for(Map.Entry<Integer, Integer> set : map.entrySet()){
            if(set.getValue() == 1){
                dummy.next = new Node(set.getKey());
                dummy = dummy.next;
            }
        }
        
        sortList(newNode.next);
        return newNode.next;
    }
    
    public static void sortList(Node head) {
        //Node current will point to head  
        Node current = head, index = null;
        int temp;

        if(head == null) {
            return;
        }
        else {
            while(current != null) {
                //Node index will point to node next to current  
                index = current.next;

                while(index != null) {
                    //If current node's data is greater than index's node data, swap the data between them  
                    if(current.data > index.data) {
                        temp = current.data;
                        current.data = index.data;
                        index.data = temp;
                    }
                    index = index.next;
                }
                current = current.next;
            }
        }
    }
+1
himanshukug19cs2 months ago
java solution
 
   //code here       Node ans=null;       Node cur=null;       HashMap<Integer ,Integer> hm = new HashMap<>();       Node node=head;        while(node!=null){            hm.putIfAbsent(node.data,0);            hm.put(node.data,hm.get(node.data)+1);            node=node.next;       }       node=head;       while(node!=null){           if(hm.get(node.data)==1){               if(ans==null){                   ans=new Node(node.data);                   cur=ans;               }               else{                   cur.next=new Node(node.data);                   cur=cur.next;               }           }           node=node.next;       }       return ans;
0
hanumanmanyam8372 months ago
public static Node removeAllDuplicates(Node head)
    {
        //code here
        Node temp=new Node(1);
        Node ptr=temp;
        if(head.data!=head.next.data)
        {
            temp.next=head;
            temp=temp.next;
        }
        Node prev=head;
        Node curr=prev.next;
        while(curr.next!=null)
        {
            if(curr.data!=prev.data && curr.data!=curr.next.data)
            {
                temp.next=new Node(curr.data);
                temp=temp.next;
            }
            prev=curr;
            curr=curr.next;
        }
        if(curr.data!=prev.data)
        {
            temp.next=new Node(curr.data);
            temp=temp.next;
        }
        return ptr.next;
        
        
    }
0
shibombhowmik12092 months ago
Node* removeAllDuplicates(struct Node* head)
    {
        //code here
        if(head==NULL or head->next==NULL){
            return head;
        }
        
        Node *dummy=new Node(0);
        dummy->next=head;
        Node *prev=dummy;
        while(head){
            if(head->next!=NULL and head->data==head->next->data){
                while(head->next!=NULL and head->data==head->next->data){
                    head=head->next;
                }
                
                prev->next=head->next;
            }
            else{
                prev=prev->next;
            }
            
            head=head->next;
        }
        
        return dummy->next;
    }
+1
chessnoobdj2 months ago
C++ best iterative 
Sadly none of the c++ solution is taking care of garbage values, it's v imp to deallocate nodes from memory so there is no memory leak and dangling pointer issue
 
Node* removeAllDuplicates(struct Node* head)
    {
        bool flg = true;
        struct Node *ptr = head, *tmp = NULL;
        //If we have duplicates from start
        if(ptr->next && ptr->data == ptr->next->data){
            while(ptr->next && ptr->data == ptr->next->data){
                Node *tmp1 = ptr->next->next, *tmp2 = ptr->next;
                delete tmp2;
                tmp2 = NULL;
                ptr->next = tmp1;
            }
            flg = false;
        }
        while(ptr){
            if(ptr->next && ptr->data == ptr->next->data){
                while(ptr->next && ptr->data == ptr->next->data){
                    Node *tmp1 = ptr->next->next, *tmp2 = ptr->next;
                    delete tmp2;
                    tmp2 = NULL;
                    ptr->next = tmp1;
                }
                tmp->next = ptr->next;
                delete ptr;
                ptr = NULL;
                ptr = tmp;
            }
            else{
                tmp = ptr;
                ptr = ptr->next;
            }
        }
        if(!flg && head){
            ptr = head;
            head = head->next;
            delete ptr;
            ptr = NULL;
        }
        return head;
    }
+1
abrajput15063 months ago
Node* removeAllDuplicates(struct Node* head)
    {
        //code here
        if(!head)
            return head;

        if(head->next && head->data == head->next->data){
            while(head->next && head->data == head->next->data){
                head = head->next;
            }
            head = head->next;
            head = removeAllDuplicates(head);    
        }else{
            head->next = removeAllDuplicates(head->next);
        }
        return head;
    }
+2
shyamprakash8075 months ago
class Solution:   def removeAllDuplicates(self, head):       #code here       if head == None or head.next == None:           return head       prev = None       curr = head       while curr != None:           temp = curr.next           delNode = None           while temp != None and temp.data == curr.data:               delNode = temp.data               temp = temp.next           if delNode == curr.data:               if prev == None:                   head = temp                   curr = temp               else:                   prev.next = temp                   curr = temp           else:               prev = curr               curr = temp       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.