VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-to-delete-all-even-nodes-from-a-singly-linked-list/

⇱ Program to delete all even nodes from a Singly Linked List - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to delete all even nodes from a Singly Linked List

Last Updated : 12 Jul, 2025

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

👁 Image


Examples:

Input: LL = 1 -> 4 -> 3 -> 18 -> 19 
Output: 1 -> 3 -> 19
Input: LL = 5 -> 3 -> 6 -> 8 -> 4 -> 1 -> 2 -> 9 
Output: 5 -> 3 -> 1 -> 9 

Approach 1:

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

Below is the implementation of the above idea:


Output
Initial List: 1 -> 4 -> 3 -> 18 -> 19 -> 
Final List: 1 -> 3 -> 19 -> 

Time Complexity: O(N^2)

As the complexity of deleteNode function is O(N) and we need to call it for every even number.  

Auxiliary Space: O(1)

As constant extra space is used.

Approach 2:

The idea is to traverse the linked list one by one and get the pointer of nodes having even values. Also keep deleting the nodes having even values using the method used in this post.

We want the time complexity to be O(1) for deleting a given node in order to get O(N) solution for overall approach.

The algorithm for the deleteNode function:

  1. In the deleteNode function we get the pointer  of the node to be deleted directly.
  2. Copy the value of next node's to this node.
  3. delete the next node.

The only thing to keep in mind is that the node to be deleted should not be the last node if we are using the above method to delete the node, but it gives the result in O(1) time so we will use this in our solution.

The algorithm for the deleteEvenNodes function:

  1. We get the head of the linked list as a function parameter.
  2. Use dummy pointer variables ptr and prev which are used to store the current and previous node respectively.
  3. Traverse the linked list before last element using a while loop and do the following:
  4.     delete the nodes with odd values and keep updating the prev and ptr pointers
  5. The case of last node is handled explicitly at the end.

Full implementation of above approach:


Output
Initial List: 18 -> 16 -> 15 -> 6 -> 2 -> 
Final List: 15 -> 

Time Complexity: O(N)

As we are visiting every node and deleting odd valued node which is O(1) operation.

Auxiliary Space: O(1)

As constant extra space is used.

Recursive Approach:

  • Base Case: If the current node is NULL, return NULL.
  • If the head is null, then return null as the list is empty.
  • If the head's value is even, then return the next node recursively.
  • If the head's value is odd, then assign the head's next to the result of the recursive call on the next node.
  • Return the head.

Below is the implementation of the above approach:

Output:

Initial List: 18 -> 16 -> 15 -> 6 -> 2 -> NULL
Final List: 15 -> NULL

Time Complexity: O(n), where n is the number of nodes in the linked list.
Space Complexity: O(n), This is because the function creates n recursive function calls on the call stack, one for each node in the linked list.

Comment
Article Tags:
Article Tags: