Title: Josephus Problem: Build the Circular Linked List
1Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt N i) Node x new Node()
x.val i x.next head
tail.next x tail x
null
0
head
2Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt N i) Node x new Node()
x.val i x.next head
tail.next x tail x
null
1
head
3Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt N i) Node x new Node()
x.val i x.next head
tail.next x tail x
1
head
4Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt N i) Node x new Node()
x.val i x.next head
tail.next x tail x
1
tail
head
5Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt N i) Node x new Node()
x.val i x.next head
tail.next x tail x
1
tail
head
6Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt N i) Node x new Node()
x.val i x.next head
tail.next x tail x
x
0
null
1
tail
head
7Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt N i) Node x new Node()
x.val i x.next head
tail.next x tail x
x
2
null
1
tail
head
8Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt N i) Node x new Node()
x.val i x.next head
tail.next x tail x
x
2
1
tail
head
9Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt N i) Node x new Node()
x.val i x.next head
tail.next x tail x
x
2
1
tail
head
10Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt N i) Node x new Node()
x.val i x.next head
tail.next x tail x
x
2
1
tail
head
11Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt N i) Node x new Node()
x.val i x.next head
tail.next x tail x
x
2
1
tail
head
12Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt N i) Node x new Node()
x.val i x.next head
tail.next x tail x
x
2
0
1
tail
null
head
13Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt N i) Node x new Node()
x.val i x.next head
tail.next x tail x
x
2
3
1
tail
null
head
14Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt N i) Node x new Node()
x.val i x.next head
tail.next x tail x
x
2
3
1
tail
head
15Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt N i) Node x new Node()
x.val i x.next head
tail.next x tail x
x
2
3
1
tail
head
16Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt N i) Node x new Node()
x.val i x.next head
tail.next x tail x
x
2
3
1
tail
head
17Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt N i) Node x new Node()
x.val i x.next head
tail.next x tail x
x
2
3
1
tail
head
18Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt N i) Node x new Node()
x.val i x.next head
tail.next x tail x
2
x
3
1
tail
0
head
null
19Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt 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
20Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt N i) Node x new Node()
x.val i x.next head
tail.next x tail x
2
x
3
1
tail
4
head
21Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt N i) Node x new Node()
x.val i x.next head
tail.next x tail x
2
x
3
1
tail
4
head
22Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt N i) Node x new Node()
x.val i x.next head
tail.next x tail x
2
x
3
1
tail
4
head
23Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt N i) Node x new Node()
x.val i x.next head
tail.next x tail x
2
3
1
4
head
tail
5
24Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt 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
25Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt 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
26Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt 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
27Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt 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
28Josephus 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(args0) int N
Integer.parseInt(args1) Node head new
Node() head.val 1 head.next
head Node tail head for (int i
2 i lt 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
29Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
N
9
M
5
java Josephus 5 9
30Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
N
9
M
5
java Josephus 5 9
31Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
N
9
M
5
java Josephus 5 9
32Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
i
1
N
9
M
5
java Josephus 5 9
33Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
i
1
N
9
M
5
java Josephus 5 9
34Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
i
2
N
9
M
5
java Josephus 5 9
35Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
i
2
N
9
M
5
java Josephus 5 9
36Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
i
3
N
9
M
5
java Josephus 5 9
37Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
i
3
N
9
M
5
java Josephus 5 9
38Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
i
4
N
9
M
5
java Josephus 5 9
39Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
i
4
N
9
M
5
java Josephus 5 9
40Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
i
5
N
9
M
5
java Josephus 5 9
41Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
N
9
M
5
java Josephus 5 9 5
42Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
N
9
M
5
java Josephus 5 9 5
43Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
N
9
M
5
java Josephus 5 9 5
44Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
i
1
N
9
M
5
java Josephus 5 9 5
45Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
i
1
N
9
M
5
java Josephus 5 9 5
46Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
i
2
N
9
M
5
java Josephus 5 9 5
47Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
i
2
N
9
M
5
java Josephus 5 9 5
48Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
i
3
N
9
M
5
java Josephus 5 9 5
49Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
i
3
N
9
M
5
java Josephus 5 9 5
50Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
i
4
N
9
M
5
java Josephus 5 9 5
51Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
i
4
N
9
M
5
java Josephus 5 9 5
52Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
i
5
N
9
M
5
java Josephus 5 9 5
53Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
N
9
M
5
java Josephus 5 9 5 1
54Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
N
9
M
5
java Josephus 5 9 5 1
55Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
N
9
M
5
java Josephus 5 9 5 1
56Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
N
9
M
5
java Josephus 5 9 5 1 7
57Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
N
9
M
5
java Josephus 5 9 5 1 7 4
58Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
N
9
M
5
java Josephus 5 9 5 1 7 4 3
59Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt 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
N
9
M
5
java Josephus 5 9 5 1 7 4 3 6
60Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt M i) x x.next
System.out.print(x.next.val " ") x.next
x.next.next System.out.println(x.val)
2
x
8
N
9
M
5
java Josephus 5 9 5 1 7 4 3 6 9
61Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt M i) x x.next
System.out.print(x.next.val " ") x.next
x.next.next System.out.println(x.val)
x
8
N
9
M
5
java Josephus 5 9 5 1 7 4 3 6 9 2
62Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt M i) x x.next
System.out.print(x.next.val " ") x.next
x.next.next System.out.println(x.val)
x
8
N
9
M
5
java Josephus 5 9 5 1 7 4 3 6 9 2
63Josephus Problem Kill Off Every Mth Person
Node x tail while (x ! x.next) for (int
i 1 i lt M i) x x.next
System.out.print(x.next.val " ") x.next
x.next.next System.out.println(x.val)
x
Survivor
8
N
9
M
5
java Josephus 5 9 5 1 7 4 3 6 9 2 8