Share my c code


  • 0
    K

    struct ListNode* removeElements(struct ListNode* head, int val) {

    struct ListNode fakehead, *current;
    
    fakehead.next = head;
    current = &fakehead;
    
    while (current->next != NULL) {
        if (current->next->val == val) {
            struct ListNode *del_node = current->next;
            current->next = current->next->next;
            free(del_node);
        }
        else {
            current = current->next;
        }
    }
    return fakehead.next;
    

    }


Log in to reply
 

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