My 21 lines code in C. only 4 ms


  • 0
    X
    struct ListNode* deleteDuplicates(struct ListNode* head) {
        struct ListNode *cur = head;
        struct ListNode store = {-1, NULL};
        struct ListNode *prev = &store;
        if (head == NULL) return NULL;
        while(cur) {
            if(cur->next && cur->val == cur->next->val) {
                struct ListNode *temp = cur->next;
                while(temp && cur->val == temp->val) {
                    temp = temp->next;
                }
                cur = temp;
            } else {
                prev->next = cur;
                prev = cur;
                cur = cur->next;            
            }
        }
        prev->next = NULL;
        return store.next;
    }

  • 0
    A

    there is a case : [1 2 3 3 3 5 6 2 2 6 5 2 ]

    the result should be : [1]

    but the result of your code is [1 2 5 6 6 5 2]


  • 0
    J

    sorted linked list


  • 0
    O
    This post is deleted!

  • 0
    S

    This is a sorted linklist...


Log in to reply
 

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