18 ms concise c++ solution

  • 0
    class Solution {
        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;
                } else {
                    p = p->next;
            return head;

  • 0

    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.