#include <iostream>
struct Node {
int val;
Node* next=nullptr;
};
Node* CreateList(size_t n)
{
Node* p = nullptr;
Node* head = nullptr;
Node* s = nullptr;
head = new Node;
p = head;
for(size_t i{}; i < n; i++)
{
s = new Node;
s->val = i + 1;
p->next = s;
p = s;
}
s->next = head->next;
delete head;
return s;
}
int main()
{
const size_t n{ 41 };
const size_t m{ 3 };
Node* p= CreateList(n);
Node* temp = nullptr;
while (p != p->next)
{
for (size_t i{ 0 }; i < m-1; i++)
{
p = p->next;
}
std::cout << "->" << p->next->val;
temp = p->next;
p->next = temp->next;
delete temp;
}
std::cout << "->"<<p->val << std::endl;
return 0;
}