Memory Efficient C++ Sol


#1

ListNode* Solution::deleteDuplicates(ListNode* A) {
ListNode *head=A;
ListNode *p=head->next;
ListNode *q=NULL;

while(head->next!=NULL){
    if(head->val==p->val){
        q=p;
        p=p->next;
        head->next=p;
        free(q);
    }
    else{
        head=head->next;
        p=p->next;
    }
}
        
return A;

}