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

class Solution {
public:
    /**
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    ListNode* deleteDuplicates(ListNode* head) {
        // write code here
        ListNode* newhead = new ListNode(0);
        newhead->next = head;

        ListNode* pre = newhead;
        ListNode* cur = head;

        int count = 0;
        while(cur != NULL && cur->next != NULL){
            if(cur->val == cur->next->val){
                cur->next = cur->next->next;
                count++;
            }
            else{
                if(count > 0){
                    pre->next = cur->next;//让链表跳过重复的节点
                    count = 0;
                }
                else{
                    pre = cur;
                }
                cur = cur->next;
            }
        }
        //判断尾节点是否需要删除
        if(count > 0)
            pre->next = cur->next;
        return newhead->next;
    }
};