Josephus Problem: Build the Circular Linked List

63
1 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; } Josephus Problem: Build the Circular Linked List 0 null head

description

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 ]); - PowerPoint PPT Presentation

Transcript of Josephus Problem: Build the Circular Linked List

Page 1: Josephus Problem:  Build the Circular Linked List

1

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; }

Josephus Problem: Build the Circular Linked List

0

null

head

Page 2: Josephus Problem:  Build the Circular Linked List

2

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; }

Josephus Problem: Build the Circular Linked List

1

null

head

Page 3: Josephus Problem:  Build the Circular Linked List

3

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; }

Josephus Problem: Build the Circular Linked List

1

head

Page 4: Josephus Problem:  Build the Circular Linked List

4

Josephus Problem: Build the Circular Linked List

1

head

tail

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; }

Page 5: Josephus Problem:  Build the Circular Linked List

5

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; }

Josephus Problem: Build the Circular Linked List

1

head

tail

Page 6: Josephus Problem:  Build the Circular Linked List

6

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; }

Josephus Problem: Build the Circular Linked List

1

head

tail

0null

x

Page 7: Josephus Problem:  Build the Circular Linked List

7

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; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2null

x

Page 8: Josephus Problem:  Build the Circular Linked List

8

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; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2

x

Page 9: Josephus Problem:  Build the Circular Linked List

9

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; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2

x

Page 10: Josephus Problem:  Build the Circular Linked List

10

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; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2

x

Page 11: Josephus Problem:  Build the Circular Linked List

11

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; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2

x

Page 12: Josephus Problem:  Build the Circular Linked List

12

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; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2

x

0

null

Page 13: Josephus Problem:  Build the Circular Linked List

13

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; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2

x

3

null

Page 14: Josephus Problem:  Build the Circular Linked List

14

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; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2

x

3

Page 15: Josephus Problem:  Build the Circular Linked List

15

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; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2

x

3

Page 16: Josephus Problem:  Build the Circular Linked List

16

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; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2

x

3

Page 17: Josephus Problem:  Build the Circular Linked List

17

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; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2

x

3

Page 18: Josephus Problem:  Build the Circular Linked List

18

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; }

Josephus Problem: Build the Circular Linked List

1

head

tail

23

0

null

x

Page 19: Josephus Problem:  Build the Circular Linked List

19

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; }

Josephus Problem: Build the Circular Linked List

1

head

tail

23

4

null

x

Page 20: Josephus Problem:  Build the Circular Linked List

20

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; }

Josephus Problem: Build the Circular Linked List

1

head

tail

23

4

x

Page 21: Josephus Problem:  Build the Circular Linked List

21

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; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2x 3

4

Page 22: Josephus Problem:  Build the Circular Linked List

22

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; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2x 3

4

Page 23: Josephus Problem:  Build the Circular Linked List

23

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; }

Josephus Problem: Build the Circular Linked List

1

head

23

4

5

tail

Page 24: Josephus Problem:  Build the Circular Linked List

24

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; }

Josephus Problem: Build the Circular Linked List

1

head

23

4

5

6

tail

Page 25: Josephus Problem:  Build the Circular Linked List

25

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; }

Josephus Problem: Build the Circular Linked List

1

head

tail

23

4

5

6 7

Page 26: Josephus Problem:  Build the Circular Linked List

26

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; }

Josephus Problem: Build the Circular Linked List

1

head

tail

23

4

5

6 7

8

Page 27: Josephus Problem:  Build the Circular Linked List

27

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; }

Josephus Problem: Build the Circular Linked List

1

head

tail

23

4

5

6 7

8

9

Page 28: Josephus Problem:  Build the Circular Linked List

28

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; }

Josephus Problem: Build the Circular Linked List

1

head

tail

23

4

5

6 7

8

9

Done Building

Page 29: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9N

9

M

5

Page 30: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9

x

N

9

M

5

Page 31: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9

x

N

9

M

5

Page 32: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9

x

i

1

N

9

M

5

Page 33: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9

x

i

1

N

9

M

5

Page 34: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9

x

i

2

N

9

M

5

Page 35: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9

x

i

2

N

9

M

5

Page 36: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9

x

i

3

N

9

M

5

Page 37: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9

x

i

3

N

9

M

5

Page 38: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9

x

i

4

N

9

M

5

Page 39: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9

x

i

4

N

9

M

5

Page 40: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9

x

i

5

N

9

M

5

Page 41: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 95

x

N

9

M

5

Page 42: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 95

x

N

9

M

5

5 is effectively deleted

Page 43: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

1

23

4

6 7

8

9

% java Josephus 5 95

x

N

9

M

5

Page 44: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

i

1

N

9

M

5

1

23

4

6 7

8

9x

% java Josephus 5 95

Page 45: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

i

1

N

9

M

5

1

23

4

6 7

8

9x

% java Josephus 5 95

Page 46: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

i

2

N

9

M

5

1

23

4

6 7

8

9x

% java Josephus 5 95

Page 47: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

i

2

N

9

M

5

1

23

4

6 7

8

9x

% java Josephus 5 95

Page 48: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

i

3

N

9

M

5

1

23

4

6 7

8

9x

% java Josephus 5 95

Page 49: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

i

3

N

9

M

5

1

23

4

6 7

8

9x

% java Josephus 5 95

Page 50: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

i

4

N

9

M

5

1

23

4

6 7

8

9x

% java Josephus 5 95

Page 51: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

i

4

N

9

M

5

1

23

4

6 7

8

9x

% java Josephus 5 95

Page 52: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

i

5

N

9

M

5

1

23

4

6 7

8

9x

% java Josephus 5 95

Page 53: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

% java Josephus 5 95 1

N

9

M

5

1

23

4

6 7

8

9x

Page 54: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

% java Josephus 5 95 1

N

9

M

5

1

23

4

6 7

8

9x

Page 55: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

% java Josephus 5 95 1

N

9

M

5

23

4

6 7

8

9x

Page 56: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

% java Josephus 5 95 1 7

N

9

M

5

23

4

6 7

8

9x

Page 57: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

% java Josephus 5 95 1 7 4

N

9

M

5

23

6

8

9x

Page 58: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

% java Josephus 5 95 1 7 4 3

N

9

M

5

2

6

8

9x

Page 59: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

% java Josephus 5 95 1 7 4 3 6

N

9

M

5

2

8

9x

Page 60: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

% java Josephus 5 95 1 7 4 3 6 9

N

9

M

5

2

8

x

Page 61: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

% java Josephus 5 95 1 7 4 3 6 9 2

N

9

M

5

8

x

Page 62: Josephus Problem:  Build the Circular Linked List

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.next;}System.out.println(x.val);

% java Josephus 5 95 1 7 4 3 6 9 2

N

9

M

5

8

x

Page 63: Josephus Problem:  Build the Circular Linked List

63

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);

% java Josephus 5 95 1 7 4 3 6 9 2 8

N

9

M

5

8

x

Survivor