Delete all the even nodes from a Doubly Linked List - GeeksforGeeks

11 Feb, 2022
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 <=> 17Explanation: 16 and 6 are even nodes. So we need to delete them.
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++
Java
Python3
C#
Javascript
// 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 liststruct Node {    int data;    Node *prev, *next;}; // function to insert a node at the beginning// of the Doubly Linked Listvoid push(Node** head_ref, int new_data){    // allocate node    Node* new_node = new Node();     // put in the data    new_node->data = new_data;     // since we are adding at the beginning,    // 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 deletedvoid 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 listvoid 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 listvoid printList(Node* head){    while (head != NULL) {        cout << head->data << " ";        head = head->next;    }} // Driver programint 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);}
// Java implementation to delete all// the even nodes from the doubly// linked listclass GFG{     // Node of the doubly linked liststatic class Node{    int data;    Node prev, next;}; // function to insert a node at the beginning// of the Doubly Linked Liststatic Node push(Node head_ref, int new_data){    // allocate node    Node new_node = new Node();     // put in the data    new_node.data = new_data;     // since we are adding at the beginning,    // 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;    return head_ref;} // function to delete a node in a Doubly Linked List.// head_ref -. pointer to head node pointer.// del -. pointer to node to be deletedstatic Node deleteNode(Node head_ref, Node del){    // base case    if (head_ref == null || del == null)        return null;     // 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;      return head_ref;} // function to delete all the even nodes// from the doubly linked liststatic Node 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;    }    return head_ref;} // function to print nodes in a// given doubly linked liststatic void printList(Node head){    while (head != null)    {        System.out.print( head.data + " ");        head = head.next;    }} // Driver codepublic static void main(String args[]){    // start with the empty list    Node head = null;     // create the doubly linked list    // 15 <. 16 <. 7 <. 6 <. 17    head = push(head, 17);    head = push(head, 6);    head = push(head, 7);    head = push(head, 16);    head = push(head, 15);     System.out.print("Original List: ");    printList(head);     head=deleteEvenNodes(head);     System.out.print( "\nModified List: ");    printList(head);}} // This code is contributed by Arnab Kundu
# Python3 implementation to delete all# the even nodes from the doubly# linked listimport math # Node of the doubly linked listclass Node:    def __init__(self, data):        self.data = data        self.next = None # function to insert a node at the beginning# of the Doubly Linked Listdef push(head_ref, new_data):         # allocate node    new_node = Node(new_data)     # put in the data    new_node.data = new_data     # since we are adding at the beginning,    # prev is always None    new_node.prev = None     # link the old list off the new node    new_node.next = head_ref     # change prev of head node to new node    if (head_ref!= None):        head_ref.prev = new_node     # move the head to point to the new node    head_ref = new_node    return head_ref # function to delete a node in a Doubly Linked List.# head_ref --> pointer to head node pointer.# del --> pointer to node to be deleteddef deleteNode(head_ref, delete):         # base case    if (head_ref == None or delete == None):        return     # If node to be deleted is head node    if (head_ref == delete):        head_ref = delete.next     # Change next only if node to be    # deleted is NOT the last node    if (delete.next != None):        delete.next.prev = delete.prev     # Change prev only if node to be    # deleted is NOT the first node    if (delete.prev != None):        delete.prev.next = delete.next     # Finally, free the memory occupied by del    return head_ref # function to delete all the even nodes# from the doubly linked listdef deleteEvenNodes(head_ref):    ptr = head_ref    next = None     while (ptr != None):        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 listdef printList(head):    while (head != None) :        print(head.data, end = " ")        head = head.next     # Driver Codeif __name__=='__main__':         # start with the empty list    head = None     # create the doubly linked list    # 15 <-> 16 <-> 7 <-> 6 <-> 17    head = push(head, 17)    head = push(head, 6)    head = push(head, 7)    head = push(head, 16)    head = push(head, 15)     print("Original List: ", end = "")    printList(head)     deleteEvenNodes(head)     print("\nModified List: ", end = "")    printList(head) # This code is contributed by Srathore
// C# implementation to delete all// the even nodes from the doubly// linked listusing System; class GFG{     // Node of the doubly linked listpublic class Node{    public int data;    public Node prev, next;}; // function to insert a node at the beginning// of the Doubly Linked Liststatic Node push(Node head_ref, int new_data){    // allocate node    Node new_node = new Node();     // put in the data    new_node.data = new_data;     // since we are adding at the beginning,    // 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;    return head_ref;} // function to delete a node in a Doubly Linked List.// head_ref -. pointer to head node pointer.// del -. pointer to node to be deletedstatic Node deleteNode(Node head_ref, Node del){    // base case    if (head_ref == null || del == null)        return null;     // 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;      return head_ref;} // function to delete all the even nodes// from the doubly linked liststatic Node 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;    }    return head_ref;} // function to print nodes in a// given doubly linked liststatic void printList(Node head){    while (head != null)    {        Console.Write( head.data + " ");        head = head.next;    }} // Driver codepublic static void Main(String []args){    // start with the empty list    Node head = null;     // create the doubly linked list    // 15 <. 16 <. 7 <. 6 <. 17    head = push(head, 17);    head = push(head, 6);    head = push(head, 7);    head = push(head, 16);    head = push(head, 15);     Console.Write("Original List: ");    printList(head);     head=deleteEvenNodes(head);     Console.Write( "\nModified List: ");    printList(head);}} /* This code is contributed by PrinciRaj1992 */
<script>// javascript implementation to delete all// the even nodes from the doubly// linked list      // Node of the doubly linked listclass Node {    constructor(val) {        this.data = val;        this.prev = null;        this.next = null;    }}// function to insert a node at the beginning// of the Doubly Linked Listfunction push(head_ref , new_data){    // allocate node    var new_node = new Node();     // put in the data    new_node.data = new_data;     // since we are adding at the beginning,    // 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;    return head_ref;} // function to delete a node in a Doubly Linked List.// head_ref -. pointer to head node pointer.// del -. pointer to node to be deletedfunction deleteNode(head_ref,  del){    // base case    if (head_ref == null || del == null)        return null;     // 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;         return head_ref;} // function to delete all the even nodes// from the doubly linked listfunction deleteEvenNodes(head_ref){    var ptr = head_ref;    var next;     while (ptr != null)    {        next = ptr.next;                 // if true, delete node 'ptr'        if (ptr.data % 2 == 0)            deleteNode(head_ref, ptr);        ptr = next;    }    return head_ref;} // function to print nodes in a// given doubly linked listfunction printList(head){    while (head != null)    {        document.write( head.data + " ");        head = head.next;    }} // Driver code      // start with the empty list    var head = null;     // create the doubly linked list    // 15 <. 16 <. 7 <. 6 <. 17    head = push(head, 17);    head = push(head, 6);    head = push(head, 7);    head = push(head, 16);    head = push(head, 15);     document.write("Original List: ");    printList(head);     head=deleteEvenNodes(head);     document.write( "<br/>Modified List: ");    printList(head);// This code contributed by Rajput-Ji</script>
Original List: 15 16 7 6 17 
Modified List: 15 7 17
 
Time Complexity: O(N), where N is the total number of nodes. Auxiliary Space: O(1) 
andrew1234
princiraj1992
Akanksha_Rai
sapnasingh4991
Rajput-Ji
pankajsharmagfg
surindertarika1234
simmytarika5
ashutoshsinghgeeksforgeeks
doubly linked list
Technical Scripter 2018
Traversal
Data Structures
Linked List
Technical Scripter
Data Structures
Linked List
Traversal
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
SDE SHEET - A Complete Guide for SDE Preparation
DSA Sheet by Love Babbar
Introduction to Algorithms
Hash Map in Python
Introduction to Tree Data Structure
Linked List | Set 1 (Introduction)
Linked List | Set 2 (Inserting a node)
Reverse a linked list
Stack Data Structure (Introduction and Program)
Linked List | Set 3 (Deleting a node)