Josephus Circle implementation using STL list | Node class to store data ; * Function to find the only person left after one in every m - th node is killed * in a circle of n nodes ; Create a circular linked list of size N . ; Connect last node to first ; * while only one node is left in the linked list ; Find m - th node ; Remove the m - th node ; Driver program to test above functions
class Node { constructor ( val ) { this . data = val ; this . next = null ; } } function getJosephusPosition ( m , n ) { var head = new Node ( 1 ) ; var prev = head ; for ( i = 2 ; i <= n ; i ++ ) { prev . next = new Node ( i ) ; prev = prev . next ; } prev . next = head ; var ptr1 = head , ptr2 = head ; while ( ptr1 . next != ptr1 ) { var count = 1 ; while ( count != m ) { ptr2 = ptr1 ; ptr1 = ptr1 . next ; count ++ ; } ptr2 . next = ptr1 . next ; ptr1 = ptr2 . next ; } document . write ( " " + " " + ptr1 . data ) ; } var n = 14 , m = 2 ; getJosephusPosition ( m , n ) ;