Both iterative and recursive solution O(N)


#1

ListNode* dup(int x,ListNode* A)
{
if(A==NULL)
return A;

 if(A->val==x){return dup(x,A->next);}
 else{A->next=dup(A->val,A->next);}
 return A;

}
ListNode* Solution::deleteDuplicates(ListNode* A) {

//////// Recurssive Solution ////////

if(A==NULL||A->next==NULL)
return A;

A->next = dup(A->val,A->next);
return A;

/////// Iterative solution ///////

ListNode *prev=A;
ListNode *curr=A->next;
while(curr)
{
    if(prev->val==curr->val)
    {prev->next=curr->next;}
    else
    {prev=curr;}
    curr=curr->next;
}
return A;

}


#2

what is mean of prev=curr.in your iterative approch.


#3

It is making pointer changing the value of prev pointer to curr pointer value .
We need to remove duplicates then if two numbers have same values we have to skip 1 vlue or remove one from the list else we will continue and store the curr value in prev because we need to check the remaining numbers too and for that we need updated prev value