Problem description ：（ Joseph Ring ）

sample input ：

The code is as follows ： Using linked list simulation
import java.util.Scanner; public class FindTheWinner { // Find the winner of the game //1 <= k <=
n <= 500 class Node{ int data; Node next; public Node(int data) { this.data =
data; } } public int findTheWinner(int n, int k) {// Use the circular list to solve the problem if (k==1) return n
; Node first=new Node(1); Node p=first; for (int i = 2; i <=n ; i++) { p.next=
new Node(i); p=p.next; } p.next=first;// end to end Node p1=first; Node pre=null; int
cnt=1; while (p1!=pre){ pre=p1; p1=p1.next; cnt++; if (cnt==k){ pre.next=p1.next
;// delete p1 Node referred to cnt=1; p1=pre.next;// take p1 Point to next node } } return p1.data; } public static
void main(String[] args) { Scanner scanner=new Scanner(System.in); int n=scanner
.nextInt(); int k=scanner.nextInt(); FindTheWinner findTheWinner=new
FindTheWinner(); System.out.println(findTheWinner.findTheWinner(n,k)); } }
The operation results are as follows ：

Technology
Daily Recommendation