Delete all odd or even positioned nodes from Circular Linked List | Link list 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 Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function Length ( head ) { var current = head ; let count = 0 ; if ( head == null ) { return 0 ; } else { do { current = current . next ; count ++ ; } while ( current != head ) ; } return count ; } function Display ( head ) { var current = head ; if ( head == null ) { document . write ( " " + " " + " " ) ; return ; } else { do { document . write ( current . data + " " ) ; current = current . next ; } while ( current != head ) ; } } function Insert ( head , data ) { var current = head ; var newNode = new Node ( ) ; if ( newNode == null ) { document . write ( " " + " " + " " ) ; 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 ; } 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 DeleteFirst ( head ) { var previous = head , next = head ; if ( head == null ) { document . write ( " " + " " + " " ) ; 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 ; } function DeleteAllOddNode ( head ) { let len = Length ( head ) ; let count = 0 ; var previous = head , next = head ; if ( head == null ) { document . write ( " " + " " + " " ) ; 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 ; } function DeleteAllEvenNode ( head ) { let len = Length ( head ) ; let count = 1 ; var previous = head , next = head ; if ( head == null ) { document . write ( " " + " " + " " ) ; 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 ; } var 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 ) ; document . write ( " " ) ; Display ( head ) ; document . write ( " " + " " ) ; head = DeleteAllOddNode ( head ) ; Display ( head ) ; document . write ( " " + " " + " " ) ; Display ( head ) ; document . write ( " " + " " ) ; head = DeleteAllEvenNode ( head ) ; Display ( head ) ;