Given a singly linked list, find if the linked list is circular or not. A linked list is called circular if it not NULL terminated and all nodes are connected in the form of a cycle. Below is an example of circular linked list.

An empty linked list is considered as circular.

Note that this problem is different from cycle detection problem, here all nodes have to be part of cycle.

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

The idea is to store head of the linked list and traverse it. If we reach NULL, linked list is not circular. If reach head again, linked list is circular.

```// C++ program to check if linked list is circular
#include<bits/stdc++.h>
using namespace std;

struct Node
{
int data;
struct Node* next;
};

/* This function returns true if given linked
list is circular, else false. */
{
// An empty linked list is circular
return true;

// This loop would stope in both cases (1) If
// Circular (2) Not circular
while (node != NULL && node != head)
node = node->next;

// If loop stopped because of circular
// condition
}

// Utility function to create a new node.
Node *newNode(int data)
{
struct Node *temp = new Node;
temp->data = data;
temp->next = NULL;
return temp;
}

/* Drier program to test above function*/
int main()
{

cout << "No\n" ;

cout << "No\n" ;

return 0;
}
```

Output :

```No
Yes```

### Asked in: MAQ Software, Microsoft

This article is contributed by Shivam Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

# GATE CS Corner    Company Wise Coding Practice

1.5 Average Difficulty : 1.5/5.0
Based on 54 vote(s)