CPP code for implementation using circular linked list


#1

#include

using namespace std;
struct Node{
int data;
Node* Next;
};

int main()
{
int n = 100;
Node* head = new Node();
head->data = 1;

Node* temp = new Node();

head->Next = temp;
for (int i = 2; i < n+1; i++)
{
    Node* temp2 = new Node();
    temp->data = i;
    (i!=n)? temp->Next = temp2 : temp->Next = head;
    temp = temp2;
}
temp = head;

while ((temp)->data != (temp->Next)->data)
{
    temp->Next = temp->Next->Next;
    temp = temp->Next;
}
cout << temp->data;

cout <<'\n';
return 0;

}