leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1823.cpp (601B)


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