public class Josephus{ static class Node{ int val; Node next; Node(int v){ val = v; } } public static void main(String[] args){ int n = Integer.parseInt(args[0]); int m = Integer.parseInt(args[1]); Node start = new Node(1); Node x = start; for(int i = 2; i <=n; i++){ x.next = new Node(i); x = x.next; } //x is last node, link x to start x.next = start; //eliminate the first node: set next reference of last node to be the second node x.next = x.next.next; while(x != x.next){ for(int i = 1; i < m; i++){ //skip m-1 nodes x = x.next; } //System.out.println("eliminating person " + x.next.val); //eliminate mth node x.next = x.next.next; } System.out.println("Survivor is person " + x.val); } }