Simple C code with 4ms


  • 0
    P
    struct ListNode* deleteDuplicates(struct ListNode* head) {
        int val = 0;
        struct ListNode ret = {0, NULL}, * p = &ret;
        while(head){
            if(head->next != NULL && head->val == head->next->val){
                val = head->val;
                while(head && head->val == val)
                    head = head->next;
            }else{
                p->next = head;
                p = head;
                head = head->next;
            }
        }
        p->next = NULL;
        return ret.next;
    }

  • 0
    P

    more concise way:
    p = p->next = head;
    though which would be too tricky.


Log in to reply
 

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