/**
 * struct ListNode {
 *    int val;
 *    struct ListNode *next;
 * };
 */

class Solution {
public:
    /**
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    ListNode* deleteDuplicates(ListNode* head) {
        // write code here
        if( nullptr==head || nullptr==(head->next) )
        {
            return head;
        }
        //改进的“双指针”
        ListNode * ret=new ListNode(-1);
        ListNode * pre=ret;
        ret->next=head;

        ListNode * Low=head;
        ListNode * Fast=head;
        while( nullptr!=Fast )
        {
            Fast=Fast->next;
            if( nullptr==Fast )
            {
                break;
            }
            int tag=0;

            while( nullptr!=Fast && Low->val!=Fast->val )
            {
                pre=pre->next;
                Low=Low->next;
                Fast=Fast->next;
            }
            while( nullptr!=Fast && Low->val == Fast->val )
            {
                Fast=Fast->next;
                tag=1;
            }

            if( tag )
            {
                pre->next=Fast;
                ListNode * temp=Low;
                Low=Fast;
                while( temp!=Fast )
                {
                    ListNode * del=temp;
                    temp=temp->next;
                    delete del;
                }

            }


            if( nullptr==Fast )
            {
                break;
            }

        }


        ListNode * temp=ret;
        ret=ret->next;
        delete temp;
        return ret;

    }
};