s4553711
4/4/2017 - 2:24 AM

82.cpp

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if (head == NULL || head->next == NULL) return head;
        ListNode *h = new ListNode(0);
        h->next = head;
        ListNode *c = h, *p = c->next;
        
        while (c->next && c->next->next) {
            if (c->next->val == c->next->next->val) {
                p = p->next;
                while (p->next && p->val == p->next->val) {
                    p = p->next;
                }
                c->next = p->next;
            } else {
                c = c->next;
                p = p->next;
            }
        }
        return h->next;
    }
};