class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        // write code here
        if(head==nullptr||head->next==nullptr)return head;
        ListNode*phead=new ListNode(INT32_MAX);
        phead->next=head;
        ListNode*p=phead->next,*pq=phead,*q=head->next;
        while(p!=nullptr&&q!=nullptr){
            if(p->val==q->val){
                int val=p->val;
                while(q&&val==q->val){
                    q=q->next;
                }
                pq->next=q;
                p=pq->next;
                if(p==nullptr)break;
                q=p->next;
            }
            else{
                pq=pq->next;
                p=pq->next;
                if(p==nullptr)break;
                q=p->next;
            }
        }
        return phead->next;
    }
};