Write a removeDuplicatesFromSortedLL() function which takes a list sorted in non-decreasing order and deletes any duplicate nodes from the list. The list should only be traversed once.
For example if the linked list is 11->11->11->21->43->43->60 then removeDuplicatesFromSortedLL() should convert the list to 11->21->43->60.
call the function with head of the linked list.
void removeDuplicatesFromSortedLL(struct node *p1)
{
struct node *p2,*temp=NULL;
int data=-1;
data=p1->data;
p2=p1->link;
while(p2)
{
if((p2->data)==data)
{
temp=p2;
p2=p2->link;
p1->link=p2;
free(temp);
temp=NULL;
}
else
{
data=p2->data;
p1=p1->link;
p2=p2->link;
}
}
}
No comments:
Post a Comment