首页 > 代码库 > 【leetcode】Remove Duplicates from Sorted List II-很精简

【leetcode】Remove Duplicates from Sorted List II-很精简

觉着自己写的比看到的答案精简,分享一下:

class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
        if(head == NULL) return NULL;
        ListNode res(-1);
        ListNode* pre = &res;
        pre->next = head;
        bool flag = true;
        while(head != NULL){
            if(head->next == NULL || head->val != head->next->val){
                if(!flag) pre->next = head->next;
                else pre = pre->next;
                flag = true;
            }else
                flag = false;
            head = head->next;
        }
        return res.next;
    }
};


【leetcode】Remove Duplicates from Sorted List II-很精简