Given a doubly linked list containing N nodes, the task is to delete all the even nodes from the list.

**Examples:**

Input:Initial List = 15 <=> 16 <=> 6 <=> 7 <=> 17

Output:Final List = 15 <=> 7 <=> 17

Input:Initial List = 5 <=> 3 <=> 6 <=> 8 <=> 4 <=> 1 <=> 2 <=> 9

Output:Final List = 5 <=> 3 <=> 1 <=> 9

The idea is to traverse the nodes of the doubly linked list one by one and get the pointer of the nodes having data even. Delete those nodes by following the approach used in this post.

Below is the implementation of above idea:

`// C++ implementation to delete all ` `// the even nodes from the doubly ` `// linked list ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// Node of the doubly linked list ` `struct` `Node { ` ` ` `int` `data; ` ` ` `Node *prev, *next; ` `}; ` ` ` `// function to insert a node at the beginning ` `// of the Doubly Linked List ` `void` `push(Node** head_ref, ` `int` `new_data) ` `{ ` ` ` `// allocate node ` ` ` `Node* new_node = (Node*)` `malloc` `(` `sizeof` `(` `struct` `Node)); ` ` ` ` ` `// put in the data ` ` ` `new_node->data = new_data; ` ` ` ` ` `// since we are adding at the begining, ` ` ` `// prev is always NULL ` ` ` `new_node->prev = NULL; ` ` ` ` ` `// link the old list off the new node ` ` ` `new_node->next = (*head_ref); ` ` ` ` ` `// change prev of head node to new node ` ` ` `if` `((*head_ref) != NULL) ` ` ` `(*head_ref)->prev = new_node; ` ` ` ` ` `// move the head to point to the new node ` ` ` `(*head_ref) = new_node; ` `} ` ` ` `// function to delete a node in a Doubly Linked List. ` `// head_ref --> pointer to head node pointer. ` `// del --> pointer to node to be deleted ` `void` `deleteNode(Node** head_ref, Node* del) ` `{ ` ` ` `// base case ` ` ` `if` `(*head_ref == NULL || del == NULL) ` ` ` `return` `; ` ` ` ` ` `// If node to be deleted is head node ` ` ` `if` `(*head_ref == del) ` ` ` `*head_ref = del->next; ` ` ` ` ` `// Change next only if node to be ` ` ` `// deleted is NOT the last node ` ` ` `if` `(del->next != NULL) ` ` ` `del->next->prev = del->prev; ` ` ` ` ` `// Change prev only if node to be ` ` ` `// deleted is NOT the first node ` ` ` `if` `(del->prev != NULL) ` ` ` `del->prev->next = del->next; ` ` ` ` ` `// Finally, free the memory occupied by del ` ` ` `free` `(del); ` ` ` ` ` `return` `; ` `} ` ` ` `// function to delete all the even nodes ` `// from the doubly linked list ` `void` `deleteEvenNodes(Node** head_ref) ` `{ ` ` ` `Node* ptr = *head_ref; ` ` ` `Node* next; ` ` ` ` ` `while` `(ptr != NULL) { ` ` ` `next = ptr->next; ` ` ` `// if true, delete node 'ptr' ` ` ` `if` `(ptr->data % 2 == 0) ` ` ` `deleteNode(head_ref, ptr); ` ` ` `ptr = next; ` ` ` `} ` `} ` ` ` `// function to print nodes in a ` `// given doubly linked list ` `void` `printList(Node* head) ` `{ ` ` ` `while` `(head != NULL) { ` ` ` `cout << head->data << ` `" "` `; ` ` ` `head = head->next; ` ` ` `} ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `// start with the empty list ` ` ` `Node* head = NULL; ` ` ` ` ` `// create the doubly linked list ` ` ` `// 15 <-> 16 <-> 7 <-> 6 <-> 17 ` ` ` `push(&head, 17); ` ` ` `push(&head, 6); ` ` ` `push(&head, 7); ` ` ` `push(&head, 16); ` ` ` `push(&head, 15); ` ` ` ` ` `cout << ` `"Original List: "` `; ` ` ` `printList(head); ` ` ` ` ` `deleteEvenNodes(&head); ` ` ` ` ` `cout << ` `"\nModified List: "` `; ` ` ` `printList(head); ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Original List: 15 16 7 6 17 Modified List: 15 7 17

**Time Complexity:** O(N), where N is the total number of nodes.

## Recommended Posts:

- Delete all the nodes from a doubly linked list that are smaller than a given value
- Delete all nodes from the doubly linked list which are divisible by K
- Delete all Prime Nodes from a Doubly Linked List
- Delete all the nodes from the doubly linked list that are greater than a given value
- Delete a node in a Doubly Linked List
- Delete all occurrences of a given key in a doubly linked list
- Delete a Doubly Linked List node at a given position
- Delete N nodes after M nodes of a linked list
- Rotate Doubly linked list by N nodes
- Delete all the even nodes of a Circular Linked List
- Delete alternate nodes of a Linked List
- Sum of all nodes in a doubly linked list divisible by a given number K
- Product of all prime nodes in a Doubly Linked List
- Delete all Prime Nodes from a Singly Linked List
- Delete all Non-Prime Nodes from a Singly Linked List

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.