Delete every Kth node from circular linked list | structure for a Node ; * Utility function to prvar the circular linked list ; Function to delete every kth Node ; If list is empty , simply return . ; take two pointers - current and previous ; Check if Node is the only Node \ If yes , we reached the goal , therefore return . ; Prvar intermediate list . ; If more than one Node present in the list , Make previous pointer povar to current Iterate current pointer k times , i . e . current Node is to be deleted . ; If Node to be deleted is head ; If Node to be deleted is last Node . ; * Function to insert a Node at the end of a Circular linked list ; Create a new Node ; if the list is empty , make the new Node head Also , it will povar to itself . ; traverse the list to reach the last Node and insert the Node ; insert Nodes in the circular linked list ; Delete every kth Node from the circular linked list .
class Node { constructor ( val ) { this . data = val ; this . next = null ; } } function printList ( head ) { if ( head == null ) return ; var temp = head ; do { document . write ( temp . data + " " ) ; temp = temp . next ; } while ( temp != head ) ; document . write ( head . data + " " ) ; } function deleteK ( head_ref , k ) { var head = head_ref ; if ( head == null ) return null ; var curr = head , prev = null ; while ( true ) { if ( curr . next == head && curr == head ) break ; printList ( head ) ; for ( i = 0 ; i < k ; i ++ ) { prev = curr ; curr = curr . next ; } if ( curr == head ) { prev = head ; while ( prev . next != head ) prev = prev . next ; head = curr . next ; prev . next = head ; head_ref = head ; } else if ( curr . next == head ) { prev . next = head ; } else { prev . next = curr . next ; } } return head ; } function insertNode ( head_ref , x ) { var head = head_ref ; var temp = new Node ( x ) ; if ( head == null ) { temp . next = temp ; head_ref = temp ; return head_ref ; } else { var temp1 = head ; while ( temp1 . next != head ) temp1 = temp1 . next ; temp1 . next = temp ; temp . next = head ; } return head ; } var head = null ; head = insertNode ( head , 1 ) ; head = insertNode ( head , 2 ) ; head = insertNode ( head , 3 ) ; head = insertNode ( head , 4 ) ; head = insertNode ( head , 5 ) ; head = insertNode ( head , 6 ) ; head = insertNode ( head , 7 ) ; head = insertNode ( head , 8 ) ; head = insertNode ( head , 9 ) ; var k = 4 ; head = deleteK ( head , k ) ;