Delete all odd nodes of a Circular Linked List
Last Updated :
10 Jun, 2022
Prerequisite: Delete all the even nodes of a Circular Linked List
Given a circular singly linked list containing N nodes, the task is to delete all the odd nodes from the list.

Examples:
Input: 572->112->21->5->1->6
Output: 572 -> 112 -> 6
Explanation: All the odd valued nodes have been deleted
Input: 9->11->32->6->13->20
Output: 32 -> 6 -> 20
Approach:
The idea is to traverse the nodes of the circular singly linked list one by one and get the pointer of the nodes having odd data. Delete those nodes by following the approach used in this post.
Below is the implementation of the above idea:
C++
// C++ program to delete all odd
// node from a Circular singly linked list
#include <bits/stdc++.h>
using namespace std;
// Structure for a node
struct Node {
int data;
struct Node* next;
};
// Function to insert a node at the beginning
// of a Circular linked list
void push(struct Node** head_ref, int data)
{
struct Node* ptr1 = (struct Node*)malloc(sizeof(struct Node));
struct Node* temp = *head_ref;
ptr1->data = data;
ptr1->next = *head_ref;
// If linked list is not NULL then
// set the next of last node
if (*head_ref != NULL) {
while (temp->next != *head_ref)
temp = temp->next;
temp->next = ptr1;
}
else
// For the first node
ptr1->next = ptr1;
*head_ref = ptr1;
}
// Delete the node if it is odd
void deleteNode(Node* head_ref, Node* del)
{
struct Node* temp = head_ref;
// If node to be deleted is head node
if (head_ref == del)
head_ref = del->next;
// Traverse list till not found
// delete node
while (temp->next != del) {
temp = temp->next;
}
// Copy address of node
temp->next = del->next;
// Finally, free the memory occupied by del
free(del);
return;
}
// Function to delete all odd nodes
// from the singly circular linked list
void deleteoddNodes(Node* head)
{
struct Node* ptr = head;
struct Node* next;
// Traverse list till the end
// if the node is odd then delete it
do {
// point to next node
next = ptr->next;
// if node is odd
if ((ptr->data % 2) == 1)
deleteNode(head, ptr);
// get the next element
ptr = next;
} while (ptr != head);
}
// Function to print nodes
void printList(struct Node* head)
{
struct Node* temp = head;
if (head != NULL) {
do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != head);
}
}
// Driver code
int main()
{
// Initialize lists as empty
struct Node* head = NULL;
// Created linked list will be 56->61->57->11->12->2
push(&head, 2);
push(&head, 12);
push(&head, 11);
push(&head, 57);
push(&head, 61);
push(&head, 56);
cout << "\nList after deletion : ";
deleteoddNodes(head);
printList(head);
return 0;
}
Java
// Java program to delete all prime
// node from a Circular singly linked list
class GFG {
// Structure for a node
static class Node {
int data;
Node next;
};
// Function to insert a node at the beginning
// of a Circular linked list
static Node push(Node head_ref, int data)
{
Node ptr1 = new Node();
Node temp = head_ref;
ptr1.data = data;
ptr1.next = head_ref;
// If linked list is not null then
// set the next of last node
if (head_ref != null) {
while (temp.next != head_ref)
temp = temp.next;
temp.next = ptr1;
return head_ref;
}
else
// For the first node
ptr1.next = ptr1;
head_ref = ptr1;
return head_ref;
}
// Delete the node if it is odd
static Node deleteNode(Node head_ref, Node del)
{
Node temp = head_ref;
// If node to be deleted is head node
if (head_ref == del)
head_ref = del.next;
// Traverse list till not found
// delete node
while (temp.next != del) {
temp = temp.next;
}
// Copy address of node
temp.next = del.next;
return head_ref;
}
// Function to delete all odd nodes
// from the singly circular linked list
static Node deleteoddNodes(Node head)
{
Node ptr = head;
Node next;
// Traverse list till the end
// if the node is odd then delete it
do {
// If node is odd
if (ptr.data % 2 == 1)
deleteNode(head, ptr);
// point to next node
next = ptr.next;
ptr = next;
} while (ptr != head);
return head;
}
// Function to print nodes
static void printList(Node head)
{
Node temp = head;
if (head != null) {
do {
System.out.printf("%d ", temp.data);
temp = temp.next;
} while (temp != head);
}
}
// Driver code
public static void main(String args[])
{
// Initialize lists as empty
Node head = null;
// Created linked list will be 56->61->57->11->12->2
head = push(head, 2);
head = push(head, 12);
head = push(head, 11);
head = push(head, 57);
head = push(head, 61);
head = push(head, 56);
System.out.println("\nList after deletion : ");
head = deleteoddNodes(head);
printList(head);
}
}
Python
# Python3 program to delete all odd
# node from a Circular singly linked list
import math
# Structure for a node
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Function to insert a node at the beginning
# of a Circular linked list
def push(head_ref, data):
ptr1 = Node(data)
temp = head_ref
ptr1.data = data
ptr1.next = head_ref
# If linked list is not None then
# set the next of last node
if (head_ref != None):
while (temp.next != head_ref):
temp = temp.next
temp.next = ptr1
else:
ptr1.next = ptr1 # For the first node
head_ref = ptr1
return head_ref
# Delete the node if it is odd
def deleteNode(head_ref, delete):
temp = head_ref
# If node to be deleted is head node
if (head_ref == delete):
head_ref = delete.next
# Traverse list till not found
# delete node
while (temp.next != delete):
temp = temp.next
# Copy address of node
temp.next = delete.next
# Function to delete all odd nodes
# from the singly circular linked list
def deleteoddNodes(head):
ptr = head
next = None
# Traverse list till the end
# if the node is odd then delete it
# if node is odd
next = ptr.next
ptr = next
while (ptr != head):
if (ptr.data % 2 == 1):
deleteNode(head, ptr)
# point to next node
next = ptr.next
ptr = next
return head
# Function to print nodes
def printList(head):
temp = head
if (head != None):
print(temp.data, end = " ")
temp = temp.next
while (temp != head):
print(temp.data, end = " ")
temp = temp.next
# Driver code
if __name__=='__main__':
# Initialize lists as empty
head = None
# Created linked list will be 56->61->57->11->12->2
head = push(head, 2)
head = push(head, 12)
head = push(head, 11)
head = push(head, 57)
head = push(head, 61)
head = push(head, 56)
print("List after deletion : ", end = "")
head = deleteoddNodes(head)
printList(head)
C#
// C# program to delete all prime
// node from a Circular singly linked list
using System;
class GFG {
// Structure for a node
public class Node {
public int data;
public Node next;
};
// Function to insert a node at the beginning
// of a Circular linked list
static Node push(Node head_ref, int data)
{
Node ptr1 = new Node();
Node temp = head_ref;
ptr1.data = data;
ptr1.next = head_ref;
// If linked list is not null then
// set the next of last node
if (head_ref != null) {
while (temp.next != head_ref)
temp = temp.next;
temp.next = ptr1;
return head_ref;
}
else
// For the first node
ptr1.next = ptr1;
head_ref = ptr1;
return head_ref;
}
// Delete the node if it is odd
static Node deleteNode(Node head_ref, Node del)
{
Node temp = head_ref;
// If node to be deleted is head node
if (head_ref == del)
head_ref = del.next;
// Traverse list till not found
// delete node
while (temp.next != del) {
temp = temp.next;
}
// copy address of node
temp.next = del.next;
return head_ref;
}
// Function to delete all odd nodes
// from the singly circular linked list
static Node deleteoddNodes(Node head)
{
Node ptr = head;
Node next;
// Traverse list till the end
// if the node is odd then delete it
do {
// If node is odd
if (ptr.data % 2 == 1)
deleteNode(head, ptr);
// Point to next node
next = ptr.next;
ptr = next;
} while (ptr != head);
return head;
}
// Function to print nodes
static void printList(Node head)
{
Node temp = head;
if (head != null) {
do {
Console.Write("{0} ", temp.data);
temp = temp.next;
} while (temp != head);
}
}
// Driver code
public static void Main(String[] args)
{
// Initialize lists as empty
Node head = null;
// Created linked list will be 56->61->57->11->12->2
head = push(head, 2);
head = push(head, 12);
head = push(head, 11);
head = push(head, 57);
head = push(head, 61);
head = push(head, 56);
Console.WriteLine("\nList after deletion : ");
head = deleteoddNodes(head);
printList(head);
}
}
JavaScript
<script>
// JavaScript program to delete all prime
// node from a Circular singly linked list
// Structure for a node
class Node {
constructor()
{
this.data = 0;
this.next = null;
}
};
// Function to insert a node at the beginning
// of a Circular linked list
function push(head_ref, data)
{
var ptr1 = new Node();
var temp = head_ref;
ptr1.data = data;
ptr1.next = head_ref;
// If linked list is not null then
// set the next of last node
if (head_ref != null) {
while (temp.next != head_ref)
temp = temp.next;
temp.next = ptr1;
return head_ref;
}
else
// For the first node
ptr1.next = ptr1;
head_ref = ptr1;
return head_ref;
}
// Delete the node if it is odd
function deleteNode(head_ref, del)
{
var temp = head_ref;
// If node to be deleted is head node
if (head_ref == del)
head_ref = del.next;
// Traverse list till not found
// delete node
while (temp.next != del) {
temp = temp.next;
}
// copy address of node
temp.next = del.next;
return head_ref;
}
// Function to delete all odd nodes
// from the singly circular linked list
function deleteoddNodes(head)
{
var ptr = head;
var next;
// Traverse list till the end
// if the node is odd then delete it
do {
// If node is odd
if (ptr.data % 2 == 1)
deleteNode(head, ptr);
// Point to next node
next = ptr.next;
ptr = next;
} while (ptr != head);
return head;
}
// Function to print nodes
function printList(head)
{
var temp = head;
if (head != null) {
do {
document.write(temp.data+ " ");
temp = temp.next;
} while (temp != head);
}
}
// Driver code
// Initialize lists as empty
var head = null;
// Created linked list will be 56->61->57->11->12->2
head = push(head, 2);
head = push(head, 12);
head = push(head, 11);
head = push(head, 57);
head = push(head, 61);
head = push(head, 56);
document.write("List after deletion : ");
head = deleteoddNodes(head);
printList(head);
</script>
Output: List after deletion : 56 12 2
Time Complexity: O(N^2)
As deleteNode function takes O(N) time and we have to process every element in worst case
Auxiliary Space: O(1)
As constant extra space is used
Similar Reads
Delete all the even nodes of a Circular Linked List Given a circular singly linked list containing N nodes, the task is to delete all the even nodes from the list. Examples: Input : 57->11->2->56->12->61 Output : List after deletion : 57 -> 11 -> 61 Input : 9->11->32->6->13->20 Output : List after deletion : 9 -
10 min read
Delete alternate nodes of a Linked List Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3. Recomm
14 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 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:  In
15+ min read
Delete N nodes after M nodes of a linked list Given a linked list and two integers m and n, the task is to traverse the linked list such that you skip m nodes, then delete the next n nodes, and continue the same till end of the linked list.Note: m cannot be 0.Example: Input: Linked List: 9->1->3->5->9->4->10->1, n = 1, m =
10 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->3
8 min read
Delete every Kth node from circular linked list Delete every kth node from a circular linked list until only one node is left. Also, print the intermediate lists. Examples: Input : n=4, k=2, list = 1->2->3->4 Output : 1->2->3->4->1 1->2->4->1 2->4->2 2->2 Input : n=9, k=4, list = 1->2->3->4->5-
13 min read
Deletion at different positions in a Circular Linked List tGiven a Circular Linked List. The task is to write programs to delete nodes from this list present at: First position.Last Position.At any given position i                                                      .Deleting first node from Singly Circular Linked List Examples:  Input : 99->11->2
15+ min read
Deletion from a Circular Linked List In this article, we will learn how to delete a node from a circular linked list. In a circular linked list, the last node connects back to the first node, creating a loop.There are three main ways to delete a node from circular linked list:Deletion at the beginningDeletion at specific positionDeleti
15+ min read