Program to delete all even nodes from a Singly Linked List
Last Updated :
28 Nov, 2023
Given a singly linked list containing N nodes, the task is to delete all the even nodes from the list.

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:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node* next;
};
void push( struct Node** head_ref,
int new_data)
{
struct Node* new_node
= ( struct Node*) malloc (
sizeof (
struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void deleteNode( struct Node** head_ref,
int key)
{
struct Node *temp = *head_ref,
*prev;
if (temp != NULL
&& temp->data == key) {
*head_ref = temp->next;
return ;
}
while (temp != NULL
&& temp->data != key) {
prev = temp;
temp = temp->next;
}
if (temp == NULL)
return ;
prev->next = temp->next;
}
void deleteEvenNodes(Node** head_ref)
{
Node* ptr = *head_ref;
while (ptr != NULL) {
if (ptr->data % 2 == 0)
deleteNode(head_ref,
ptr->data);
ptr = ptr->next;
}
}
void printList( struct Node* node)
{
while (node != NULL) {
printf ( " %d -> " , node->data);
node = node->next;
}
}
int main()
{
Node* head = NULL;
push(&head, 19);
push(&head, 18);
push(&head, 3);
push(&head, 4);
push(&head, 1);
printf ( "Initial List: " );
printList(head);
deleteEvenNodes(&head);
printf ( "\nFinal List: " );
printList(head);
}
|
Java
class LinkedList{
Node head;
class Node
{
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
public void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
void deleteNode( int key)
{
Node temp = head, prev = null ;
if (temp != null && temp.data == key)
{
head = temp.next;
return ;
}
while (temp != null && temp.data != key)
{
prev = temp;
temp = temp.next;
}
if (temp == null ) return ;
prev.next = temp.next;
}
void deleteEvenNodes()
{
Node ptr = head;
while (ptr != null )
{
if (ptr.data % 2 == 0 )
{
deleteNode(ptr.data);
}
ptr = ptr.next;
}
}
public void printList()
{
Node ptr = head;
while (ptr != null )
{
System.out.print(ptr.data + "-> " );
ptr = ptr.next;
}
}
public static void main(String[] args)
{
LinkedList head = new LinkedList();
head.push( 19 );
head.push( 18 );
head.push( 3 );
head.push( 4 );
head.push( 1 );
System.out.print( "\nInitial List: " );
head.printList();
head.deleteEvenNodes();
System.out.print( "\nFinal List: " );
head.printList();
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def push( self , new_data):
new_node = Node(new_data)
new_node. next = self .head
self .head = new_node
def printList( self ):
ptr = self .head
while (ptr ! = None ):
print (ptr.data, '-> ' , end = '')
ptr = ptr. next
print ()
def deleteNode( self , key):
temp = self .head
if (temp ! = None and temp.data = = key):
self .head = temp. next
return
while (temp ! = None and temp.data ! = key):
prev = temp
temp = temp. next
if (temp = = None ):
return
prev. next = temp. next
def deleteEvenNodes( self ):
ptr = self .head
while (ptr ! = None ):
if (ptr.data % 2 = = 0 ):
self .deleteNode(ptr.data)
ptr = ptr. next
if __name__ = = '__main__' :
head = LinkedList()
head.push( 19 )
head.push( 18 )
head.push( 3 )
head.push( 4 )
head.push( 1 )
print ( "Initial list: " , end = '')
head.printList()
head.deleteEvenNodes()
print ( "Final list: " , end = '')
head.printList()
|
C#
using System;
class List{
Node head;
public class Node
{
public int data;
public Node next;
public Node( int d)
{
data = d;
next = null ;
}
}
public void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
void deleteNode( int key)
{
Node temp = head, prev = null ;
if (temp != null &&
temp.data == key)
{
head = temp.next;
return ;
}
while (temp != null &&
temp.data != key)
{
prev = temp;
temp = temp.next;
}
if (temp == null )
return ;
prev.next = temp.next;
}
void deleteEvenNodes()
{
Node ptr = head;
while (ptr != null )
{
if (ptr.data % 2 == 0)
{
deleteNode(ptr.data);
}
ptr = ptr.next;
}
}
public void printList()
{
Node ptr = head;
while (ptr != null )
{
Console.Write(ptr.data + "-> " );
ptr = ptr.next;
}
}
public static void Main(String []args)
{
List head = new List();
head.push(19);
head.push(18);
head.push(3);
head.push(4);
head.push(1);
Console.Write( "\nInitial List: " );
head.printList();
head.deleteEvenNodes();
Console.Write( "\nFinal List: " );
head.printList();
}
}
|
Javascript
<script>
var head;
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function push(new_data) {
var new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
function deleteNode(key) {
var temp = head, prev = null ;
if (temp != null && temp.data == key) {
head = temp.next;
return ;
}
while (temp != null && temp.data != key) {
prev = temp;
temp = temp.next;
}
if (temp == null )
return ;
prev.next = temp.next;
}
function deleteEvenNodes() {
var ptr = head;
while (ptr != null ) {
if (ptr.data % 2 == 0) {
deleteNode(ptr.data);
}
ptr = ptr.next;
}
}
function printList() {
var ptr = head;
while (ptr != null ) {
document.write(ptr.data + "-> " );
ptr = ptr.next;
}
}
push(19);
push(18);
push(3);
push(4);
push(1);
document.write( "<br/>Initial List: " );
printList();
deleteEvenNodes();
document.write( "<br/>Final List: " );
printList();
</script>
|
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:
- In the deleteNode function we get the pointer of the node to be deleted directly.
- Copy the value of next node’s to this node.
- 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:
- We get the head of the linked list as a function parameter.
- Use dummy pointer variables ptr and prev which are used to store the current and previous node respectively.
- Traverse the linked list before last element using a while loop and do the following:
- delete the nodes with odd values and keep updating the prev and ptr pointers
- The case of last node is handled explicitly at the end.
Full implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node
= ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void deleteNode( struct Node* node)
{
node->data = node->next->data;
struct Node* temp = node->next;
node->next = node->next->next;
delete (temp);
}
void deleteEvenNodes(Node** head_ref)
{
Node* ptr = *head_ref;
Node* prev;
while (ptr != NULL && ptr->next != NULL) {
if (ptr->data % 2 == 0)
deleteNode(ptr);
else {
prev = ptr;
ptr = ptr->next;
}
}
if (ptr == *head_ref && ptr->data % 2 == 0)
*head_ref = NULL;
else if (ptr->data % 2 == 0) {
prev->next = NULL;
delete ptr;
}
}
void printList( struct Node* node)
{
while (node != NULL) {
printf ( " %d -> " , node->data);
node = node->next;
}
}
int main()
{
Node* head = NULL;
push(&head, 2);
push(&head, 6);
push(&head, 15);
push(&head, 16);
push(&head, 18);
printf ( "Initial List: " );
printList(head);
deleteEvenNodes(&head);
printf ( "\nFinal List: " );
printList(head);
}
|
Java
class LinkedList{
Node head;
class Node
{
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
public void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
void deleteNode(Node node)
{
node.data = node.next.data;
node.next = node.next.next;
}
void deleteEvenNodes()
{
Node ptr = head;
Node prev = null ;
while (ptr!= null && ptr.next != null )
{
if (ptr.data % 2 == 0 )
deleteNode(ptr);
else {
prev = ptr;
ptr = ptr.next;
}
}
if (ptr==head && ptr.data % 2 == 0 )
head = null ;
else if (ptr.data % 2 == 0 )
prev.next = null ;
}
public void printList()
{
Node ptr = head;
while (ptr != null )
{
System.out.print(ptr.data + "-> " );
ptr = ptr.next;
}
}
public static void main(String[] args)
{
LinkedList head = new LinkedList();
head.push( 2 );
head.push( 6 );
head.push( 15 );
head.push( 16 );
head.push( 18 );
System.out.print( "\nInitial List: " );
head.printList();
head.deleteEvenNodes();
System.out.print( "\nFinal List: " );
head.printList();
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def push( self , new_data):
new_node = Node(new_data)
new_node. next = self .head
self .head = new_node
def printList( self ):
ptr = self .head
while (ptr ! = None ):
print (ptr.data, '-> ' , end = '')
ptr = ptr. next
print ()
def deleteNode( self ,node):
node.data = node. next .data
node. next = node. next . next
def deleteEvenNodes( self ):
ptr = self .head
prev = self .head
while (ptr! = None and ptr. next ! = None ):
if (ptr.data % 2 = = 0 ):
self .deleteNode(ptr)
else :
prev = ptr
ptr = ptr. next
if (ptr = = self .head and ptr.data % 2 = = 0 ):
head = None
elif (ptr.data % 2 = = 0 ):
prev. next = None
if __name__ = = '__main__' :
head = LinkedList()
head.push( 18 )
head.push( 16 )
head.push( 15 )
head.push( 6 )
head.push( 2 )
print ( "Initial list: " , end = '')
head.printList()
head.deleteEvenNodes()
print ( "Final list: " , end = '')
head.printList()
|
C#
using System;
class List{
Node head;
public class Node
{
public int data;
public Node next;
public Node( int d)
{
data = d;
next = null ;
}
}
public void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
void deleteNode(Node node)
{
node.data = node.next.data;
node.next = node.next.next;
}
void deleteEvenNodes()
{
Node ptr = head;
Node prev = null ;
while (ptr!= null && ptr.next != null )
{
if (ptr.data % 2 == 0)
deleteNode(ptr);
else {
prev = ptr;
ptr = ptr.next;
}
}
if (ptr==head && ptr.data % 2 == 0)
head = null ;
else if (ptr.data % 2 == 0)
prev.next = null ;
}
public void printList()
{
Node ptr = head;
while (ptr != null )
{
Console.Write(ptr.data + "-> " );
ptr = ptr.next;
}
}
public static void Main(String []args)
{
List head = new List();
head.push(2);
head.push(6);
head.push(15);
head.push(16);
head.push(18);
Console.Write( "\nInitial List: " );
head.printList();
head.deleteEvenNodes();
Console.Write( "\nFinal List: " );
head.printList();
}
}
|
Javascript
<script>
var head;
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function push(new_data) {
var new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
void deleteNode(Node node)
{
node.data = node.next.data;
node.next = node.next.next;
}
function deleteEvenNodes() {
var ptr = head,prev = null ;
while (ptr!= null && ptr.next != null )
{
if (ptr.data % 2 == 0)
deleteNode(ptr);
else {
prev = ptr;
ptr = ptr.next;
}
}
if (ptr==head && ptr.data % 2 == 0)
head = null ;
else if (ptr.data % 2 == 0)
prev.next = null ;
}
function printList() {
var ptr = head;
while (ptr != null ) {
document.write(ptr.data + "-> " );
ptr = ptr.next;
}
}
push(2);
push(6);
push(15);
push(16);
push(18);
document.write( "<br/>Initial List: " );
printList();
deleteEvenNodes();
document.write( "<br/>Final List: " );
printList();
</script>
|
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:
C++
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void deleteEvenNodes( struct Node** head_ref)
{
if (*head_ref == NULL) {
return ;
}
if ((*head_ref)->data % 2 == 0) {
struct Node* temp = *head_ref;
*head_ref = (*head_ref)->next;
delete (temp);
deleteEvenNodes(head_ref);
}
else {
deleteEvenNodes(&((*head_ref)->next));
}
}
void printList( struct Node* node)
{
while (node != NULL) {
cout << node->data << " -> " ;
node = node->next;
}
cout << "NULL" << endl;
}
int main()
{
Node* head = NULL;
push(&head, 2);
push(&head, 6);
push(&head, 15);
push(&head, 16);
push(&head, 18);
cout << "Initial List: " ;
printList(head);
deleteEvenNodes(&head);
cout << "Final List: " ;
printList(head);
return 0;
}
|
Java
public class GFG {
static class Node {
int data;
Node next;
Node( int data)
{
this .data = data;
this .next = null ;
}
}
static Node push(Node head, int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
return new_node;
}
static Node deleteEvenNodes(Node head)
{
Node temp = head;
Node prev = null ;
while (temp != null && temp.data % 2 == 0 ) {
head = temp.next;
temp = head;
}
while (temp != null ) {
while (temp != null && temp.data % 2 != 0 ) {
prev = temp;
temp = temp.next;
}
if (temp == null ) {
return head;
}
prev.next = temp.next;
temp = prev.next;
}
return head;
}
static void printList(Node node)
{
while (node != null ) {
System.out.print(node.data + " -> " );
node = node.next;
}
System.out.println( "NULL" );
}
public static void main(String[] args)
{
Node head = null ;
head = push(head, 2 );
head = push(head, 6 );
head = push(head, 15 );
head = push(head, 16 );
head = push(head, 18 );
System.out.print( "Initial List: " );
printList(head);
head = deleteEvenNodes(head);
System.out.print( "Final List: " );
printList(head);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def push(head_ref, new_data):
new_node = Node(new_data)
new_node. next = head_ref
head_ref = new_node
return head_ref
def deleteEvenNodes(head_ref):
if head_ref is None :
return None
if head_ref.data % 2 = = 0 :
temp = head_ref
head_ref = head_ref. next
del temp
head_ref = deleteEvenNodes(head_ref)
else :
head_ref. next = deleteEvenNodes(head_ref. next )
return head_ref
def printList(node):
while node is not None :
print (node.data, end = " -> " )
node = node. next
print ( "NULL" )
if __name__ = = "__main__" :
head = None
head = push(head, 2 )
head = push(head, 6 )
head = push(head, 15 )
head = push(head, 16 )
head = push(head, 18 )
print ( "Initial List: " , end = "")
printList(head)
head = deleteEvenNodes(head)
print ( "Final List: " , end = "")
printList(head)
|
C#
using System;
public class Node {
public int data;
public Node next;
public Node( int data)
{
this .data = data;
this .next = null ;
}
}
public class LinkedList {
public static void Push( ref Node headRef, int newData)
{
Node newNode = new Node(newData);
newNode.next = headRef;
headRef = newNode;
}
public static void DeleteEvenNodes( ref Node headRef)
{
if (headRef == null ) {
return ;
}
if (headRef.data % 2 == 0) {
_ = headRef;
headRef = headRef.next;
_ = null ;
DeleteEvenNodes( ref headRef);
}
else {
DeleteEvenNodes( ref headRef.next);
}
}
public static void PrintList(Node node)
{
while (node != null ) {
Console.Write(node.data + " -> " );
node = node.next;
}
Console.WriteLine( "NULL" );
}
public static void Main( string [] args)
{
Node head = null ;
Push( ref head, 2);
Push( ref head, 6);
Push( ref head, 15);
Push( ref head, 16);
Push( ref head, 18);
Console.Write( "Initial List: " );
PrintList(head);
DeleteEvenNodes( ref head);
Console.Write( "Final List: " );
PrintList(head);
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .next = null ;
}
}
function push(head, new_data) {
let new_node = new Node(new_data);
new_node.next = head;
return new_node;
}
function deleteEvenNodes(head) {
if (head === null ) {
return null ;
}
if (head.data % 2 === 0) {
let temp = head.next;
delete head;
return deleteEvenNodes(temp);
} else {
head.next = deleteEvenNodes(head.next);
return head;
}
}
function printList(node) {
while (node !== null ) {
console.log(node.data + " -> " );
node = node.next;
}
console.log( "NULL" );
}
function main() {
let head = null ;
head = push(head, 2);
head = push(head, 6);
head = push(head, 15);
head = push(head, 16);
head = push(head, 18);
console.log( "Initial List: " );
printList(head);
head = deleteEvenNodes(head);
console.log( "Final List: " );
printList(head);
}
main();
|
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.
Similar Reads
Delete all Prime Nodes from a Singly Linked List
Given a singly linked list containing N nodes, the task is to delete all nodes from the list which are prime.Examples: Input : List = 15 -> 16 -> 6 -> 7 -> 17 Output : Final List = 15 -> 16 -> 6Input : List = 15 -> 3 -> 4 -> 2 -> 9 Output :Final List = 15 ->4 -> 9
12 min read
Delete all Prime Nodes from a Circular Singly Linked List
Given a circular singly linked list containing N nodes. The task is to delete all nodes from the list which are prime. Examples: Input : 9->11->32->6->13->20 Output : 9 32 6 20 Input : 6->11->16->21->17->10 Output : 6 16 21 10 Approach: The idea is to traverse the nodes
13 min read
Delete all the even nodes from a Doubly Linked List
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.
11 min read
Delete all odd or even positioned nodes from Circular Linked List
Delete all odd or even position nodes from circular linked list Given a Singly Circular Linked List, starting from the first node delete all odd position nodes in it. Note: Linked list is considered to have 1-based indexing. That is, first element in the linked list is at position 1. Examples: Input
15+ min read
Remove all the Even Digit Sum Nodes from a Circular Singly Linked List
Given a circular singly linked list containing N nodes, the task is to remove all the nodes from the list which contains elements whose digit sum is even. Examples: Input: CLL = 9 -> 11 -> 34 -> 6 -> 13 -> 21 Output: 9 -> 34 -> 21 Explanation: The circular singly linked list con
12 min read
Delete all Prime Nodes from a Doubly Linked List
Given a doubly linked list containing N nodes, the task is to delete all nodes from the list which are prime. Examples: Input: List = 15 <=> 16 <=> 6 <=> 7 <=> 17 Output: Final List = 15 <=> 16 <=> 6 Input: List = 5 <=> 3 <=> 4 <=> 2 <=> 9
13 min read
Delete a Linked List node at a given position
Given a singly linked list and a position (1-based indexing), the task is to delete a linked list node at the given position. Note: Position will be valid (i.e, 1 <= position <= linked list length) Example: Input: position = 2, Linked List = 8->2->3->1->7Output: Linked List = 8-
8 min read
Remove all even parity nodes from a Doubly and Circular Singly Linked List
Given a Doubly linked list and Circular singly linked list containing N nodes, the task is to remove all the nodes from each list which contains elements whose parity is even. Example: Input: CLL = 9 -> 11 -> 34 -> 6 -> 13 -> 21 Output: 11 -> 13 -> 21 Explanation: The circular s
15+ min read
Delete a Node in a Singly Linked List with Only a Pointer
Given only a pointer to a node to be deleted in a singly linked list. The task is to delete that node from the list. Note: The given pointer does not point to the last node of the linked list. Examples: Input: LinkedList: 1->2->3->4->5, delete_ptr = 2Output: LinkedList: 1->3->4-
6 min read
Alternate Odd and Even Nodes in a Singly Linked List
Given a singly linked list, rearrange the list so that even and odd nodes are alternate in the list.There are two possible forms of this rearrangement. If the first data is odd, then the second node must be even. The third node must be odd and so on. Notice that another arrangement is possible where
15+ min read