clean c++ solution


  • 0
    D
    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) {
            if (!head) return head;
            ListNode *dummy = new ListNode(0);
            dummy->next = head;
            ListNode *prev = dummy;
            ListNode *cur = head;
            while (cur){
                ListNode *dup_ptr = cur->next;    
                bool is_dup = false;
                while (dup_ptr && dup_ptr->val == cur->val){
                    auto temp = dup_ptr;
                    dup_ptr = dup_ptr->next;
                    delete temp;
                    is_dup = true;
                }
                if (is_dup){
                    prev->next = dup_ptr;
                    delete cur;
                    cur = dup_ptr;
                }else{
                    prev = cur;
                    cur = cur->next;
                }
            }
            auto ret = dummy->next;
            delete dummy;
            return ret;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.