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

来源:互联网 发布:dfu刷机会清空数据吗 编辑:程序博客网 时间:2024/06/06 01:28

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

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


0 0
原创粉丝点击