Simple C++ code 13ms.


  • 1
    G
    class Solution {
    public:
        ListNode *deleteDuplicates(ListNode *head) {
            if(!head)
                return NULL;
           if( head->next == NULL)
                return head;
    
            ListNode *p = head,*tmp = NULL;
            while(p){
                tmp = p;
                p = p->next;
                while(p&&p -> val == tmp->val)
                    p = p->next;
                tmp->next = p;
            }
            return head;
        }
    };

  • 3
    L
        ……
        ListNode *p = head, *tmp = NULL, *del = NULL;
        while (p) {
            tmp = p;
            p = p->next;
            while (p && p->val == tmp->val) {
                del = p;
                p = p->next;
                delete del;
            }
            tmp->next = p;
        }
        ……

  • 0
    Z

    Yeah, we should delete the duplicate ListNode; otherwise we are leaking memory.


Log in to reply
 

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