C++ simple implementation


#1
ListNode* Solution::deleteDuplicates(ListNode* A) {
ListNode* head = A;
if(!head)return A;
int prev_value=A->val;
ListNode* prev=head;
ListNode* node = head->next;
if(!node)return A;
while(node){
    if(node->val==prev_value)
    {
        prev->next=node->next;
        
    }
    else{
    prev=node;
    prev_value=node->val;
    }
    node=node->next;
}
return head;

}


#2

freeing the extra memory of deleted node :-
ListNode* Solution::deleteDuplicates(ListNode* A) {
ListNode* head = A;
if(!head)return A;
int prev_value=A->val;
ListNode* prev=head;
ListNode* node = head->next;
if(!node)return A;
ListNode* del_node;
while(node){
if(node->val==prev_value)
{
prev->next=node->next;del_node=node;
node=node->next;
if(del_node)free(del_node);
}
else{
prev=node;
prev_value=node->val;
node=node->next;
}

}
return head;

}