Program to remove duplicate elements from the sorted linked list
Iam going to give just the function of above program but not the whole program.The function is explained at each line using comment line.
void removeduplicates(struct node* head)
{
struct node* curr=head; // Make head as current node
struct node node_next_to_next;
if(curr==NULL)
return;
while(curr->next!=NULL) //Iterate up to the end of the linked list
{
if(curr->data==curr->next->data) // compare the data of node to its next node in the linked list
{
node_next_to_next=curr->next->next; // if its equal then make next_to_next node as curr->next->next
free(curr->next); // and delete the curr->next node
curr->next=node_next_to_next; // Now make curr->next node as node_next_to_next.
} // Thus removing the duplicate element.
else
curr=curr->next; // If its not equal just make curr as curr->next
}
}
Input:
4->12->12->13->14->14->16
Output:
4->12->13->14->16
Comments
Post a Comment