Skip to content

Latest commit

 

History

History
34 lines (25 loc) · 873 Bytes

Delete occurence of given node.md

File metadata and controls

34 lines (25 loc) · 873 Bytes
void deleteAllOccurOfX(struct Node** head, int x) {
    if (*head == NULL)
        return;

    // Handle the case where the value to delete is at the beginning of the list
    while (*head != NULL && (*head)->data == x) {
        struct Node* temp = *head;
        *head = (*head)->next;
        delete temp;
    }

    struct Node* p = *head;

    while (p != NULL) {
        struct Node* nn = p->next;

        if (p->data == x) {
           
            p->prev->next = nn;
            
            // If the node to delete is at the end of the list
            if (nn != NULL)
                nn->prev = p->prev;

            delete p;
        }
        p = nn;
    }
}