public class Josephs {
static class Node{
Node next;
int data;
public Node(int data){
this.data=data;
}
}
public static int josephs(Node head,int n){
if (head.next==null){
return head.data;
}
Node last=head;
while(last.next!=head){
last=last.next;
}
int count = 0 ;
while(head!=last){
if (++count==n){
last.next=head.next;
count=0;
}else {
last=last.next;
}
head=last.next;
}
return last.data;
}
public static void main(String[] args) {
Node head1 = new Node(1);
head1.next = new Node(2);
head1.next.next = new Node(3);
head1.next.next.next = new Node(4);
head1.next.next.next.next = new Node(5);
head1.next.next.next.next.next = head1;
System.out.println(josephs(head1, 3));
}
}