Josephus Problem Build the Circular Linked List public
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = null 0 head new Node(); 1; head; for (int i = 2; i <= N; i++) { Node x = new Node(); x. val = i; x. next = head; tail. next = x; tail = x; } 1
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = null 1 head new Node(); 1; head; for (int i = 2; i <= N; i++) { Node x = new Node(); x. val = i; x. next = head; tail. next = x; tail = x; } 2
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = 1 head new Node(); 1; head; for (int i = 2; i <= N; i++) { Node x = new Node(); x. val = i; x. next = head; tail. next = x; tail = x; } 3
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = 1 tail head new Node(); 1; head; for (int i = 2; i <= N; i++) { Node x = new Node(); x. val = i; x. next = head; tail. next = x; tail = x; } 4
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = 1 tail head new Node(); 1; head; for (int i = 2; i <= N; i++) { Node x = new Node(); x. val = i; x. next = head; tail. next = x; tail = x; } 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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = x 0 null 1 tail head new Node(); 1; head; for (int i = 2; i <= N; i++) { Node x = new Node(); x. val = i; x. next = head; tail. next = x; tail = x; } 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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = x 2 null 1 tail head new Node(); 1; head; for (int i = 2; i <= N; i++) { Node x = new Node(); x. val = i; x. next = head; tail. next = x; tail = x; } 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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = x 2 1 tail head new Node(); 1; head; for (int i = 2; i <= N; i++) { Node x = new Node(); x. val = i; x. next = head; tail. next = x; tail = x; } 8
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = x 2 1 tail head new Node(); 1; head; for (int i = 2; i <= N; i++) { Node x = new Node(); x. val = i; x. next = head; tail. next = x; tail = x; } 9
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = x 2 1 tail head new Node(); 1; head; for (int i = 2; i <= N; i++) { Node x = new Node(); x. val = i; x. next = head; tail. next = x; tail = x; } 10
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = x 2 1 tail head new Node(); 1; head; for (int i = 2; i <= N; i++) { Node x = new Node(); x. val = i; x. next = head; tail. next = x; tail = x; } 11
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = x 2 0 1 tail null head new Node(); 1; head; for (int i = 2; i <= N; i++) { Node x = new Node(); x. val = i; x. next = head; tail. next = x; tail = x; } 12
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = x 2 3 1 tail null head new Node(); 1; head; for (int i = 2; i <= N; i++) { Node x = new Node(); x. val = i; x. next = head; tail. next = x; tail = x; } 13
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = x 2 3 1 tail head new Node(); 1; head; for (int i = 2; i <= N; i++) { Node x = new Node(); x. val = i; x. next = head; tail. next = x; tail = x; } 14
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = x 2 3 1 tail head new Node(); 1; head; for (int i = 2; i <= N; i++) { Node x = new Node(); x. val = i; x. next = head; tail. next = x; tail = x; } 15
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = x 2 3 1 tail head new Node(); 1; head; for (int i = 2; i <= N; i++) { Node x = new Node(); x. val = i; x. next = head; tail. next = x; tail = x; } 16
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = x 2 3 1 tail head new Node(); 1; head; for (int i = 2; i <= N; i++) { Node x = new Node(); x. val = i; x. next = head; tail. next = x; tail = x; } 17
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = new Node(); 1; head; x 2 3 1 tail 0 head null for (int i = 2; i <= N; i++) { Node x = new Node(); x. val = i; x. next = head; tail. next = x; tail = x; } 18
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = new Node(); 1; head; x 2 3 1 tail 4 head null for (int i = 2; i <= N; i++) { Node x = new Node(); x. val = i; x. next = head; tail. next = x; tail = x; } 19
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = x 2 3 1 tail 4 head new Node(); 1; head; for (int i = 2; i <= N; i++) { Node x = new Node(); x. val = i; x. next = head; tail. next = x; tail = x; } 20
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = x 2 3 1 tail 4 head new Node(); 1; head; for (int i = 2; i <= N; i++) { Node x = new Node(); x. val = i; x. next = head; tail. next = x; tail = x; } 21
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = x 2 3 1 tail 4 head new Node(); 1; head; for (int i = 2; i <= N; i++) { Node x = new Node(); x. val = i; x. next = head; tail. next = x; tail = x; } 22
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = new Node(); 1; head; 3 2 1 4 head tail 5 for (int i = 2; i <= N; i++) { Node x = new Node(); x. val = i; x. next = head; tail. next = x; tail = x; } 23
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = new Node(); 1; 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 24
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = new Node(); 1; 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 25
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = new Node(); 1; 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 26
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = new Node(); 1; 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 27
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. parse. Int(args[0]); int N = Integer. parse. Int(args[1]); Node head. val head. next Node tail = = new Node(); 1; 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 28
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; } System. out. println(x. val); 2 3 1 4 9 5 8 6 % java Josephus 5 9 M N 5 9 7 29
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; } System. out. println(x. val); 2 3 1 4 x 9 5 8 6 % java Josephus 5 9 M N 5 9 7 30
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; } System. out. println(x. val); 2 3 1 4 x 9 5 8 6 % java Josephus 5 9 M N 5 9 7 31
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; } System. out. println(x. val); 2 3 1 4 x 9 5 8 6 % java Josephus 5 9 7 M N i 5 9 1 32
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; } System. out. println(x. val); 2 3 1 4 x 9 5 8 6 % java Josephus 5 9 7 M N i 5 9 1 33
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; } System. out. println(x. val); 2 3 1 4 x 9 5 8 6 % java Josephus 5 9 7 M N i 5 9 2 34
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; } System. out. println(x. val); 2 3 1 4 x 9 5 8 6 % java Josephus 5 9 7 M N i 5 9 2 35
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; } System. out. println(x. val); 2 3 1 4 x 9 5 8 6 % java Josephus 5 9 7 M N i 5 9 3 36
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; } System. out. println(x. val); 2 3 1 4 x 9 5 8 6 % java Josephus 5 9 7 M N i 5 9 3 37
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; } System. out. println(x. val); 2 3 1 4 x 9 5 8 6 % java Josephus 5 9 7 M N i 5 9 4 38
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; } System. out. println(x. val); 2 3 1 4 x 9 5 8 6 % java Josephus 5 9 7 M N i 5 9 4 39
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; } System. out. println(x. val); 2 3 1 4 x 9 5 8 6 % java Josephus 5 9 7 M N i 5 9 5 40
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; } System. out. println(x. val); 2 3 1 4 x 9 5 8 6 % java Josephus 5 9 5 M N 5 9 7 41
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; } System. out. println(x. val); 2 3 1 4 x 5 is effectively deleted 5 8 6 % java Josephus 5 9 M N 5 9 7 42
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; } System. out. println(x. val); 2 3 1 4 x 9 8 6 % java Josephus 5 9 5 M N 5 9 7 43
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; } System. out. println(x. val); 2 3 1 4 x 9 8 6 % java Josephus 5 9 5 7 M N i 5 9 1 44
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; } System. out. println(x. val); 2 3 1 4 x 9 8 6 % java Josephus 5 9 5 7 M N i 5 9 1 45
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; } System. out. println(x. val); 2 3 1 4 x 9 8 6 % java Josephus 5 9 5 7 M N i 5 9 2 46
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; } System. out. println(x. val); 2 3 1 4 x 9 8 6 % java Josephus 5 9 5 7 M N i 5 9 2 47
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; } System. out. println(x. val); 2 3 1 4 x 9 8 6 % java Josephus 5 9 5 7 M N i 5 9 3 48
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; } System. out. println(x. val); 2 3 1 4 x 9 8 6 % java Josephus 5 9 5 7 M N i 5 9 3 49
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; } System. out. println(x. val); 2 3 1 4 x 9 8 6 % java Josephus 5 9 5 7 M N i 5 9 4 50
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; } System. out. println(x. val); 2 3 1 4 x 9 8 6 % java Josephus 5 9 5 7 M N i 5 9 4 51
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; } System. out. println(x. val); 2 3 1 4 x 9 8 6 % java Josephus 5 9 5 7 M N i 5 9 5 52
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; } System. out. println(x. val); 2 3 1 4 x 9 8 6 % java Josephus 5 9 5 1 M N 5 9 7 53
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; } System. out. println(x. val); 2 3 1 4 x 9 8 6 % java Josephus 5 9 5 1 M N 5 9 7 54
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; } System. out. println(x. val); 2 3 4 x 9 8 6 % java Josephus 5 9 5 1 M N 5 9 7 55
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; } System. out. println(x. val); 2 3 4 x 9 8 6 % java Josephus 5 9 5 1 7 M N 5 9 7 56
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; } System. out. println(x. val); 2 3 x 9 8 6 % java Josephus 5 9 5 1 7 4 M N 5 9 57
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; } System. out. println(x. val); 2 x 9 8 6 % java Josephus 5 9 5 1 7 4 3 M N 5 9 58
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; } System. out. println(x. val); 2 x 9 8 % java Josephus 5 9 5 1 7 4 3 6 M N 5 9 59
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; } System. out. println(x. val); 2 x 8 % java Josephus 5 9 5 1 7 4 3 6 9 M N 5 9 60
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; } System. out. println(x. val); x 8 % java Josephus 5 9 5 1 7 4 3 6 9 2 M N 5 9 61
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; } System. out. println(x. val); x 8 % java Josephus 5 9 5 1 7 4 3 6 9 2 M N 5 9 62
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; } System. out. println(x. val); x Survivor % java Josephus 5 9 5 1 7 4 3 6 9 2 8 M N 5 9 8 63
- Slides: 63