#include <bits/stdc++.h>
using namespace std;
struct Node {
int n;
struct Node* next;
};
bool isCircular(struct Node *head) {
if (head == NULL)
return true;
struct Node *nodeNext = head->next;
while (nodeNext != NULL && nodeNext != head)
nodeNext = nodeNext->next;
return (nodeNext == head);
}
Node *addNewNode(int data) {
struct Node *newNode = new Node;
newNode->n = data;
newNode->next = NULL;
return newNode;
}
void deleteLinkedList(struct Node *head) {
struct Node *next;
while (head != NULL) {
next = head->next;
free(head);
head = next;
}
}
int main()
{
struct Node* head = addNewNode(1);
head->next = addNewNode(2);
head->next->next = addNewNode(3);
isCircular(head)? cout << "Yes\n" : cout << "No\n" ;
head->next->next->next = addNewNode(4);
head->next->next->next->next = head;
isCircular(head)? cout << "Yes\n" : cout << "No\n" ;
head->next->next->next->next = NULL;
isCircular(head)? cout << "Yes\n" : cout << "No\n" ;
deleteLinkedList(head);
return 0;
}
/*
run:
No
Yes
No
*/