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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
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 (*head_ref != NULL) {
while (temp->next != *head_ref)
temp = temp->next;
temp->next = ptr1;
}
else
ptr1->next = ptr1;
*head_ref = ptr1;
}
void deleteNode(Node* head_ref, Node* del)
{
struct Node* temp = head_ref;
if (head_ref == del)
head_ref = del->next;
while (temp->next != del) {
temp = temp->next;
}
temp->next = del->next;
free (del);
return ;
}
void deleteoddNodes(Node* head)
{
struct Node* ptr = head;
struct Node* next;
do {
next = ptr->next;
if ((ptr->data % 2) == 1)
deleteNode(head, ptr);
ptr = next;
} while (ptr != head);
}
void printList( struct Node* head)
{
struct Node* temp = head;
if (head != NULL) {
do {
printf ( "%d " , temp->data);
temp = temp->next;
} while (temp != head);
}
}
int main()
{
struct Node* head = NULL;
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
class GFG {
static class Node {
int data;
Node next;
};
static Node push(Node head_ref, int data)
{
Node ptr1 = new Node();
Node temp = head_ref;
ptr1.data = data;
ptr1.next = head_ref;
if (head_ref != null ) {
while (temp.next != head_ref)
temp = temp.next;
temp.next = ptr1;
return head_ref;
}
else
ptr1.next = ptr1;
head_ref = ptr1;
return head_ref;
}
static Node deleteNode(Node head_ref, Node del)
{
Node temp = head_ref;
if (head_ref == del)
head_ref = del.next;
while (temp.next != del) {
temp = temp.next;
}
temp.next = del.next;
return head_ref;
}
static Node deleteoddNodes(Node head)
{
Node ptr = head;
Node next;
do {
if (ptr.data % 2 == 1 )
deleteNode(head, ptr);
next = ptr.next;
ptr = next;
} while (ptr != head);
return head;
}
static void printList(Node head)
{
Node temp = head;
if (head != null ) {
do {
System.out.printf( "%d " , temp.data);
temp = temp.next;
} while (temp != head);
}
}
public static void main(String args[])
{
Node head = null ;
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
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def push(head_ref, data):
ptr1 = Node(data)
temp = head_ref
ptr1.data = data
ptr1. next = head_ref
if (head_ref ! = None ):
while (temp. next ! = head_ref):
temp = temp. next
temp. next = ptr1
else :
ptr1. next = ptr1
head_ref = ptr1
return head_ref
def deleteNode(head_ref, delete):
temp = head_ref
if (head_ref = = delete):
head_ref = delete. next
while (temp. next ! = delete):
temp = temp. next
temp. next = delete. next
def deleteoddNodes(head):
ptr = head
next = None
next = ptr. next
ptr = next
while (ptr ! = head):
if (ptr.data % 2 = = 1 ):
deleteNode(head, ptr)
next = ptr. next
ptr = next
return head
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
if __name__ = = '__main__' :
head = None
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#
using System;
class GFG {
public class Node {
public int data;
public Node next;
};
static Node push(Node head_ref, int data)
{
Node ptr1 = new Node();
Node temp = head_ref;
ptr1.data = data;
ptr1.next = head_ref;
if (head_ref != null ) {
while (temp.next != head_ref)
temp = temp.next;
temp.next = ptr1;
return head_ref;
}
else
ptr1.next = ptr1;
head_ref = ptr1;
return head_ref;
}
static Node deleteNode(Node head_ref, Node del)
{
Node temp = head_ref;
if (head_ref == del)
head_ref = del.next;
while (temp.next != del) {
temp = temp.next;
}
temp.next = del.next;
return head_ref;
}
static Node deleteoddNodes(Node head)
{
Node ptr = head;
Node next;
do {
if (ptr.data % 2 == 1)
deleteNode(head, ptr);
next = ptr.next;
ptr = next;
} while (ptr != head);
return head;
}
static void printList(Node head)
{
Node temp = head;
if (head != null ) {
do {
Console.Write( "{0} " , temp.data);
temp = temp.next;
} while (temp != head);
}
}
public static void Main(String[] args)
{
Node head = null ;
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>
class Node {
constructor()
{
this .data = 0;
this .next = null ;
}
};
function push(head_ref, data)
{
var ptr1 = new Node();
var temp = head_ref;
ptr1.data = data;
ptr1.next = head_ref;
if (head_ref != null ) {
while (temp.next != head_ref)
temp = temp.next;
temp.next = ptr1;
return head_ref;
}
else
ptr1.next = ptr1;
head_ref = ptr1;
return head_ref;
}
function deleteNode(head_ref, del)
{
var temp = head_ref;
if (head_ref == del)
head_ref = del.next;
while (temp.next != del) {
temp = temp.next;
}
temp.next = del.next;
return head_ref;
}
function deleteoddNodes(head)
{
var ptr = head;
var next;
do {
if (ptr.data % 2 == 1)
deleteNode(head, ptr);
next = ptr.next;
ptr = next;
} while (ptr != head);
return head;
}
function printList(head)
{
var temp = head;
if (head != null ) {
do {
document.write(temp.data+ " " );
temp = temp.next;
} while (temp != head);
}
}
var head = null ;
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
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!