Delete all the even nodes of a Circular Linked List | Python3 program to delete all even node from a Circular singly linked list ; Structure for a node ; Function to insert a node at the beginning of a Circular linked list ; If linked list is not None then set the next of last node ; For the first node ; Delete the node if it is even ; If node to be deleted is head node ; traverse list till not found delete node ; copy address of node ; Function to delete all even nodes from the singly circular linked list ; if node is even ; po to next node ; Function to pr nodes ; Driver code ; Initialize lists as empty ; Created linked list will be 57.11 .2 .56 .12 .61
import math NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def push ( head_ref , data ) : NEW_LINE INDENT ptr1 = Node ( data ) NEW_LINE temp = head_ref NEW_LINE ptr1 . data = data NEW_LINE ptr1 . next = head_ref NEW_LINE if ( head_ref != None ) : NEW_LINE INDENT while ( temp . next != head_ref ) : NEW_LINE INDENT temp = temp . next NEW_LINE DEDENT temp . next = ptr1 NEW_LINE DEDENT else : NEW_LINE INDENT ptr1 . next = ptr1 NEW_LINE DEDENT head_ref = ptr1 NEW_LINE return head_ref NEW_LINE DEDENT def deleteNode ( head_ref , delete ) : NEW_LINE INDENT temp = head_ref NEW_LINE if ( head_ref == delete ) : NEW_LINE INDENT head_ref = delete . next NEW_LINE DEDENT while ( temp . next != delete ) : NEW_LINE INDENT temp = temp . next NEW_LINE DEDENT temp . next = delete . next NEW_LINE DEDENT def deleteEvenNodes ( head ) : NEW_LINE INDENT ptr = head NEW_LINE next = None NEW_LINE next = ptr . next NEW_LINE ptr = next NEW_LINE while ( ptr != head ) : NEW_LINE INDENT if ( ptr . data % 2 == 0 ) : NEW_LINE INDENT deleteNode ( head , ptr ) NEW_LINE DEDENT next = ptr . next NEW_LINE ptr = next NEW_LINE DEDENT return head NEW_LINE DEDENT def prList ( head ) : NEW_LINE INDENT temp = head NEW_LINE if ( head != None ) : NEW_LINE INDENT print ( temp . data , end = " ▁ " ) NEW_LINE temp = temp . next NEW_LINE while ( temp != head ) : NEW_LINE INDENT print ( temp . data , end = " ▁ " ) NEW_LINE temp = temp . next NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = push ( head , 61 ) NEW_LINE head = push ( head , 12 ) NEW_LINE head = push ( head , 56 ) NEW_LINE head = push ( head , 2 ) NEW_LINE head = push ( head , 11 ) NEW_LINE head = push ( head , 57 ) NEW_LINE print ( " List ▁ after ▁ deletion ▁ : ▁ " , end = " " ) NEW_LINE head = deleteEvenNodes ( head ) NEW_LINE prList ( head ) NEW_LINE DEDENT