Remove Duplicates from Sorted List II

/**
 * 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 prehead(0);
        ListNode *p,*pre;
        prehead.next=head;
        pre=&prehead;
        while(head&&head->next)
        {
            if(head->val==head->next->val) 
            {
               // node1->next=head;
                while(head->next&&head&&head->val==head->next->val) head=head->next;
                pre->next=head=head->next; 
             }
             else
             {
                 pre=head;
                 if(head->next==NULL)return prehead.next;
                 else  head=head->next;
                 
             }
            
            
        }
        
       return prehead.next;
    }
};

8min,但之前尝试释放指针却没有成功

原文地址:https://www.cnblogs.com/daocaorenblog/p/4886080.html