Remove duplicate element from sorted Linked List | Practice | GeeksforGeeks

Given a singly linked list consisting of N nodes. The task is to remove duplicates (nodes with duplicate values) from the given list (if exists).
Note: Try not to use extra space. Expected time complexity is O(N). The nodes are arranged in a sorted way.
Example 1:
Input:
LinkedList: 2->2->4->5
Output: 2 4 5
Explanation: In the given linked list 
2 ->2 -> 4-> 5, only 2 occurs more 
than 1 time.

Example 2:
Input:
LinkedList: 2->2->2->2->2
Output: 2
Explanation: In the given linked list 
2 ->2 ->2 ->2 ->2, 2 is the only element
and is repeated 5 times.
Your Task:
The task is to complete the function removeDuplicates() which should remove the duplicates from linked list and return the head of the linkedlist.
Expected Time Complexity : O(N)
Expected Auxilliary Space : O(1)
Constraints:
1 <= Number of nodes <= 104
 
+1
rohitsingh325 days ago
Node *removeDuplicates(Node *head)
{
    Node* curr = head;
    while(curr->next != NULL){
        if(curr->data == curr->next->data){
            curr->next = curr->next->next;
        }else{
            curr = curr->next;
        }
    }
    return head;
    
 
}
0
arjunaryagupta2036 days ago
#Python Code
 
    #code here   if head.next==None or head==None :       return head   else:       prev=head       nxt=head.next              while nxt!=None:           if prev.data!=nxt.data:               prev=prev.next               nxt=nxt.next           elif prev.data==nxt.data:
               temp=nxt.next               prev.next=temp               nxt=temp       return head           
+1
sdakshya231 week ago

0.06sec probably one the best solution:
Node *curr = head;
    while(curr->next != NULL){
        if(curr->data == curr->next->data){
            Node *temp = curr->next;
            curr->next = curr->next->next;
            delete temp;
        }
        else{
            curr = curr->next;
        }
        
    }
    return head;
+1
shahabuddinbravo402 weeks ago
// ptr1 is one step ahead of ptr;
Node *removeDuplicates(Node *head){  Node *ptr=head,*ptr1=head->next;  while(ptr!=NULL && ptr1!=NULL)  {      if(ptr->data==ptr1->data){          ptr1=ptr1->next;          ptr->next=ptr1;      }      else{          ptr=ptr->next;          ptr1=ptr1->next;      }  }    return head;  }
0
gcharansaicis192 weeks ago
void removeDuplicates(struct Node* head){   int prev = head->data;   struct Node* temp2 = head->next;   struct Node* temp1 = head;   while(temp2!=NULL&&temp1!=NULL)   {       if(temp2->data==prev)       {           temp1->next = temp2->next;           temp2->next = NULL;           temp2 = temp1->next;       }       else       {              prev = temp2->data;           temp1 = temp1->next;           temp2 = temp2->next;       }   }   return head;   }
0
alokkumar90192 weeks ago
Node *removeDuplicates(Node *head)
{
// your code goes here
Node *curr=head;
while(curr!=NULL && curr->next!=NULL){
    if(curr->data == curr->next->data){
        Node *temp=curr->next;
        curr->next=curr->next->next;
        delete(temp);
    } else{
        curr=curr->next;
    }
}
return head;
}
+1
jainmuskan5652 weeks ago
void removeDuplicates(struct Node* head){
   while(head->next!= NULL){       if(head->data== head->next->data){           head->next= head->next->next;       }       else{           head=head->next;       }   }   }
 
agar same nhi hoga tabhi head ko increment karege nhi to bs next hi aage bhadega
 
0
midas123 weeks ago
Node *removeDuplicates(Node *head){// your code goes hereNode *t1 = head;Node *t2 = head;while(t1->next){    if (t1->data == t1->next->data){        t2 = t1->next->next;        t1->next = t2;            }    else t1 = t1->next;    }return head;
0
midas123 weeks ago
cpp solution:
 
Node *removeDuplicates(Node *head){// your code goes hereNode *t1 = head;Node *t2 = head;while(t1->next){    if (t1->data == t1->next->data){        t2 = t1->next->next;        t1->next = t2;            }    else t1 = t1->next;    }return head;
+1
2019sushilkumarkori3 weeks ago
Node *removeDuplicates(Node *head)
{
 // your code goes here
    Node* curr=head;
    while(curr->next!=NULL){
        if(curr->data==curr->next->data){
            curr->next = curr->next->next;
        }
        else 
        {
            curr = curr->next;
        }
    }
    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.