Remove duplicates from a sorted linked list | GeeksforGeeks
http://comproguide.blogspot.com/2014/05/deleting-duplicate-elements-from-sorted.html
Write a removeDuplicates() 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.
Traverse the list from the head (or start) node. While traversing, compare each node with its next node. If data of next node is same as current node then delete the next node. Before we delete a node, we need to store next pointer of the node
Read full article from Remove duplicates from a sorted linked list | GeeksforGeeks
http://comproguide.blogspot.com/2014/05/deleting-duplicate-elements-from-sorted.html
Write a removeDuplicates() 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.
Traverse the list from the head (or start) node. While traversing, compare each node with its next node. If data of next node is same as current node then delete the next node. Before we delete a node, we need to store next pointer of the node
void
removeDuplicates(
struct
node* head)
{
/* Pointer to traverse the linked list */
struct
node* current = head;
/* Pointer to store the next pointer of a node to be deleted*/
struct
node* next_next;
/* do nothing if the list is empty */
if
(current == NULL)
return
;
/* Traverse the list till last node */
while
(current->next != NULL)
{
/* Compare current node with next node */
if
(current->data == current->next->data)
{
/*The sequence of steps is important*/
next_next = current->next->next;
free
(current->next);
current->next = next_next;
}
else
/* This is tricky: only advance if no deletion */
{
current = current->next;
}
}
}