leetcode 82 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||!head->next) return head;
        ListNode ln(0);ln.next=head;
        ListNode* node=head,*pre=&ln,*cur=node->next;
        while(cur) {
            if(cur->val==node->val) {
                 while(cur&&cur->val==node->val) {
                    ListNode* tmp=cur;
                    cur=cur->next;
                    delete tmp;
                }//如果每次都修改node->next指针的话,更费时;
                pre->next=cur;
                delete node;
                node=pre->next;
                if(node) cur=node->next;
            }//if
           else {
               pre=node;
               node=cur;
               cur=cur->next;
           }
        }
        return ln.next;
    }
};
原文地址:https://www.cnblogs.com/LiuQiujie/p/12613265.html