18 ms concise c++ solution


  • 0
    P
    class Solution {
    public:
        ListNode *deleteDuplicates(ListNode *head) {
            if (head == NULL) {
                return NULL;
            }
            
            ListNode* p = head;
            while(p->next) {
                if (p->next->val == p->val) {
                    ListNode* del = p->next;
                    p->next = p->next->next;
                    delete(del);
                } else {
                    p = p->next;
                }
            }
            return head;
        }
    };

  • 0
    A

    I have a quick question. Why can you use "delete" here? I thought "delete" is only applied when the memory was created on the stack. In other word, it is always corresponding to a "new". Thanks for sharing the answer.


Log in to reply
 

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