import java.util.LinkedList; publicclassSolution { publicintLastRemaining_Solution(int n, int m) { if (n == 0 || m == 0) return -1; LinkedList<Integer> list=newLinkedList<>(); for (inti=0; i < n; i++) { list.add(i); }
intindex=0; while (list.size() > 1) { index = (index + m - 1) % list.size(); list.remove(index); } return list.size() == 1 ? list.get(0) : -1; } }