My 4 ms C solution


  • 1
    X
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    //delete the next node of P
    void DeleteNextNode(struct ListNode* L,struct ListNode* P)
    {
    struct ListNode* tem =NULL;
    if(P->next->next != NULL)
    {
        tem = P->next;
        P->next = tem->next;
        free(tem);
    }
    else
       P->next = NULL; 
    }
    
    struct ListNode* deleteDuplicates(struct ListNode* head) {
    if(head == NULL)
        return NULL;
    struct ListNode* P = head;
    while(P->next != NULL)
    {
        if(P->val == P->next->val)
            DeleteNextNode(head,P);
        if(P->next != NULL && P->val != P->next->val)
            P = P->next;
        else
            continue;
     }
     return head;
    }

Log in to reply
 

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