1823.cpp (601B)
1 class Solution { 2 struct Node { 3 Node *next; 4 int val; 5 Node(int val = -1, Node *next = nullptr) : val(val), next(next) {} 6 }; 7 8 public: 9 int findTheWinner(int n, int k) { 10 Node *h, *t; 11 t = h = new Node(); 12 for (int i = 1; i <= n; i++) 13 t = t->next = new Node(i); 14 t->next = h->next; 15 delete h; 16 17 while (t != t->next) { 18 for (int c = k - 1; c; c--) 19 t = t->next; 20 h = t->next; 21 t->next = t->next->next; 22 delete h; 23 } 24 25 return t->val; 26 } 27 };