Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; null head
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; null 1 head
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 1 head
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 1 tail head
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 1 tail head
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x null 1 tail head
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x 2 null 1 tail head
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x 2 1 tail head
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x 2 1 tail head
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x 2 1 tail head
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x 2 1 tail head
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x 2 1 tail null head
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x 2 3 1 tail null head
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x 2 3 1 tail head
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x 2 3 1 tail head
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x 2 3 1 tail head
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x 2 3 1 tail head
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 2 x 3 1 tail head null
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 2 x 3 1 tail 4 head null
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 2 x 3 1 tail 4 head
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 2 x 3 1 tail 4 head
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 2 x 3 1 tail 4 head
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 2 3 1 4 head tail 5
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 2 3 1 4 head tail 5 6
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 2 3 1 4 head tail 5 6 7
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 2 3 1 4 head tail 5 8 6 7
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 2 3 1 4 head 9 tail 5 8 6 7
Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 2 3 1 4 head 9 tail 5 8 6 7 Done Building
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 9 5 8 6 7 M 5 N 9 % java Josephus 5 9
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 % java Josephus 5 9
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 % java Josephus 5 9
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 i 1 % java Josephus 5 9
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 i 1 % java Josephus 5 9
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 i 2 % java Josephus 5 9
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 i 2 % java Josephus 5 9
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 i 3 % java Josephus 5 9
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 i 3 % java Josephus 5 9
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 i 4 % java Josephus 5 9
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 i 4 % java Josephus 5 9
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 i 5 % java Josephus 5 9
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 % java Josephus 5 9 5
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 is effectively deleted 5 8 6 7 M 5 N 9 % java Josephus 5 9 5
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 % java Josephus 5 9 5
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 i 1 % java Josephus 5 9 5
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 i 1 % java Josephus 5 9 5
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 i 2 % java Josephus 5 9 5
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 i 2 % java Josephus 5 9 5
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 i 3 % java Josephus 5 9 5
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 i 3 % java Josephus 5 9 5
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 i 4 % java Josephus 5 9 5
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 i 4 % java Josephus 5 9 5
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 i 5 % java Josephus 5 9 5
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 % java Josephus 5 9 5 1
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 % java Josephus 5 9 5 1
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 4 x 9 8 6 7 M 5 N 9 % java Josephus 5 9 5 1
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 4 x 9 8 6 7 M 5 N 9 % java Josephus 5 9 5 1 7
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 x 9 8 6 M 5 N 9 % java Josephus 5 9 5 1 7 4
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 x 9 8 6 M 5 N 9 % java Josephus 5 9 5 1 7 4 3
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 x 9 8 M 5 N 9 % java Josephus 5 9 5 1 7 4 3 6
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 x 8 M 5 N 9 % java Josephus 5 9 5 1 7 4 3 6 9
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); x 8 M 5 N 9 % java Josephus 5 9 5 1 7 4 3 6 9 2
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); x 8 M 5 N 9 % java Josephus 5 9 5 1 7 4 3 6 9 2
Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); x Survivor 8 M 5 N 9 % java Josephus 5 9 5 1 7 4 3 6 9 2 8