C solution, using double pointer


  • 0
    L
    struct ListNode* removeElements(struct ListNode* head, int val) {
        struct ListNode **ptr = &head, *tmp;
        
        while(*ptr) {
            while((*ptr) && ((*ptr)->val != val)) ptr = &((*ptr)->next);
            if(*ptr) {
                tmp = *ptr;
                *ptr = (*ptr)->next;
                free(tmp);
            }
        }
        return head;
    }
    

Log in to reply
 

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