my c++ solution


  • 0
    2

    for a node of a list ,such as pre->p->nxt, only pre->val!=p->val && p->val!=nxt->val;
    the node p can be added to the result.

    class Solution {
    public:
    ListNode* deleteDuplicates(ListNode* head) {
    ListNode* pre=NULL;
    ListNode* nxt=NULL,* p=head;
    ListNode* start=new ListNode(0),*h=start;
    if(!head||!head->next)
    return head;
    while(p)
    {
    nxt=p->next;
    // if the node is duplicate, just skip
    if((nxt&&p->val==nxt->val)||(pre&&pre->val==p->val))
    {
    pre=p;
    p=p->next;
    }else
    {
    pre=p;
    start->next=p;
    p=p->next;
    start=start->next;
    start->next=NULL;
    }
    }

       return h->next;
     
        
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.