【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;
    }
};


猜你喜欢

转载自blog.csdn.net/cx351864995/article/details/39126881