Delete all odd or even positioned nodes from Circular Linked List | Java program to delete all even and odd position nodes from Singly Circular Linked list ; structure for a node ; Function return number of nodes present in list ; if list is empty simply return length zero ; traverse forst to last node ; Function print data of list ; if list is empty simply show message ; traverse forst to last node ; Function to insert a node at the end of a Circular linked list ; Create a new node ; check node is created or not ; insert data into newly created node ; check list is empty if not have any node then make first node it ; if list have already some node ; move firt node to last node ; put first or head node address in new node link ; put new node address into last node link ( next ) ; Utitlity function to delete a Node ; If node to be deleted is head node ; traverse list till not found delete node ; copy address of node ; Function to delete First node of Circular Linked List ; check list have any node if not then return ; check list have single node if yes then delete it and return ; traverse second to first ; now previous is last node and next is first node of list first node ( next ) link address put in last node ( previous ) link ; make second node as head node ; Function to delete odd position nodes ; check list have any node if not then return ; if list have single node means odd position then delete it ; traverse first to last if list have more than one node ; delete first position node which is odd position ; Function to delete first node ; check position is odd or not if yes then delete node Note : Considered 1 based indexing ; Function to delete all even position nodes ; Take size of list ; Check list is empty if empty simply return ; if list have single node then return ; make first node is previous ; make second node is current ; check node number is even if node is even then delete that node ; Driver Code ; Deleting Odd positioned nodes ; Deleting Even positioned nodes
class GFG { static class Node { int data ; Node next ; } ; static int Length ( Node head ) { Node current = head ; int count = 0 ; if ( head == null ) { return 0 ; } else { do { current = current . next ; count ++ ; } while ( current != head ) ; } return count ; } static void Display ( Node head ) { Node current = head ; if ( head == null ) { System . out . printf ( " Display List is empty "); return ; } else { do { System . out . printf ( " % d ▁ " , current . data ) ; current = current . next ; } while ( current != head ) ; } } static Node Insert ( Node head , int data ) { Node current = head ; Node newNode = new Node ( ) ; if ( newNode == null ) { System . out . printf ( " Memory Error "); return null ; } newNode . data = data ; if ( head == null ) { newNode . next = newNode ; head = newNode ; return head ; } else { while ( current . next != head ) { current = current . next ; } newNode . next = head ; current . next = newNode ; } return head ; } 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 DeleteFirst ( Node head ) { Node previous = head , next = head ; if ( head == null ) { System . out . printf ( " List is empty "); return head ; } if ( previous . next == previous ) { head = null ; return head ; } while ( previous . next != head ) { previous = previous . next ; next = previous . next ; } previous . next = next . next ; head = previous . next ; return head ; } static Node DeleteAllOddNode ( Node head ) { int len = Length ( head ) ; int count = 0 ; Node previous = head , next = head ; if ( head == null ) { System . out . printf ( " Delete Last List is empty "); return null ; } if ( len == 1 ) { head = DeleteFirst ( head ) ; return head ; } while ( len > 0 ) { if ( count == 0 ) { head = DeleteFirst ( head ) ; } if ( count % 2 == 0 && count != 0 ) { head = deleteNode ( head , previous ) ; } previous = previous . next ; next = previous . next ; len -- ; count ++ ; } return head ; } static Node DeleteAllEvenNode ( Node head ) { int len = Length ( head ) ; int count = 1 ; Node previous = head , next = head ; if ( head == null ) { System . out . printf ( " List is empty "); return null ; } if ( len < 2 ) { return null ; } previous = head ; next = previous . next ; while ( len > 0 ) { if ( count % 2 == 0 ) { previous . next = next . next ; previous = next . next ; next = previous . next ; } len -- ; count ++ ; } return head ; } public static void main ( String args [ ] ) { Node head = null ; head = Insert ( head , 99 ) ; head = Insert ( head , 11 ) ; head = Insert ( head , 22 ) ; head = Insert ( head , 33 ) ; head = Insert ( head , 44 ) ; head = Insert ( head , 55 ) ; head = Insert ( head , 66 ) ; System . out . printf ( " Initial ▁ List : ▁ " ) ; Display ( head ) ; System . out . printf ( " After deleting Odd position nodes : "); head = DeleteAllOddNode ( head ) ; Display ( head ) ; System . out . printf ( " Initial List : "); Display ( head ) ; System . out . printf ( " After deleting even position nodes : "); head = DeleteAllEvenNode ( head ) ; Display ( head ) ; } }