/* remove duplicate elements from sorted link list*/
#include<stdio.h>
#include<stdlib.h>
/* Link list node */
struct node{
int data;
struct node* next;
};
/* Function to get the middle of the linked list*/
void RemoveDuplicates_from_sortedList(struct node **head)
{
struct node *temp=*head;
struct node *del;
while(((temp)!=NULL)&&((temp)->next!=NULL))
{
if((temp->data)==(temp->next->data))
{
del=temp->next;
temp->next=del->next;
free(del);
temp=*head;
}
else
{
temp=temp->next;
}
}
}
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
// A utility function to print a given linked list
void printList(struct node *ptr)
{
while (ptr != NULL)
{
printf("%d->", ptr->data);
ptr = ptr->next;
}
printf("NULL\n");
}
int main()
{
/* Start
/* Start with the empty list */
struct node* head = NULL;
int i;
push(&head,3);
push(&head,3);
push(&head,2);
push(&head,2);
push(&head,2);
push(&head,1);
push(&head, 1);
push(&head,1);
push(&head,1);
push(&head,1);
printf("Sorted List is :->");
printList(head);
RemoveDuplicates_from_sortedList(&head);
printf("\n\nAfter removing duplicate from sorted List is: ->");
printList(head);
return 0;
}