Delete duplicate value nodes from a sorted linked list Solution

Delete duplicate value nodes from a sorted linked list Solution


Problem Statement

You're given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete as few nodes as possible so that the list does not contain any value more than once. The given head pointer may be null indicating that the list is empty.

Source code:

 /*  
  Remove all duplicate elements from a sorted linked list  
  Node is defined as   
  struct Node  
  {  
    int data;  
    struct Node *next;  
  }  
 */  
 Node* RemoveDuplicates(Node *head)  
 {  
  // This is a "method-only" submission.   
  // You only need to complete this method.   
   Node *cur = head;  
   while(cur->next!=NULL){  
     if(cur->data == cur->next->data)  
       cur->next = cur->next->next;   
     else  
     cur = cur->next;  
   }  
   return head;  
 }  

6 comments :