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 , here all nodes have to be part of cycle.

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++

 `// C++ program to check if linked list is circular ` `#include ` `using` `namespace` `std; ` ` `  `/* Link list Node */` `struct` `Node ` `{ ` `    ``int` `data; ` `    ``struct` `Node* next; ` `}; ` ` `  `/* This function returns true if given linked ` `   ``list is circular, else false. */` `bool` `isCircular(``struct` `Node *head) ` `{ ` `    ``// An empty linked list is circular ` `    ``if` `(head == NULL) ` `       ``return` `true``; ` ` `  `    ``// Next of head ` `    ``struct` `Node *node = head->next; ` ` `  `    ``// 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 ` `    ``return` `(node == head); ` `} ` ` `  `// 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() ` `{ ` `    ``/* Start with the empty list */` `    ``struct` `Node* head = newNode(1); ` `    ``head->next = newNode(2); ` `    ``head->next->next = newNode(3); ` `    ``head->next->next->next = newNode(4); ` ` `  `    ``isCircular(head)? cout << ````"Yes "``` `: ` `                      ``cout << ````"No "``` `; ` ` `  `    ``// Making linked list circular ` `    ``head->next->next->next->next = head; ` ` `  `    ``isCircular(head)? cout << ````"Yes "``` `: ` `                      ``cout << ````"No "``` `; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to check if  ` `// linked list is circular ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` `     `  `/* Link list Node */` `static` `class` `Node ` `{ ` `    ``int` `data; ` `    ``Node next; ` `} ` ` `  `/*This function returns true if given linked ` `list is circular, else false. */` `static` `boolean` `isCircular( Node head) ` `{ ` `    ``// An empty linked list is circular ` `    ``if` `(head == ``null``) ` `    ``return` `true``; ` ` `  `    ``// Next of head ` `    ``Node node = head.next; ` ` `  `    ``// 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 ` `    ``return` `(node == head); ` `} ` ` `  `// Utility function to create a new node. ` `static` `Node newNode(``int` `data) ` `{ ` `    ``Node temp = ``new` `Node(); ` `    ``temp.data = data; ` `    ``temp.next = ``null``; ` `    ``return` `temp; ` `} ` ` `  `/* Driver code*/` `public` `static` `void` `main(String args[]) ` `{ ` `    ``/* Start with the empty list */` `    ``Node head = newNode(``1``); ` `    ``head.next = newNode(``2``); ` `    ``head.next.next = newNode(``3``); ` `    ``head.next.next.next = newNode(``4``); ` ` `  `    ``System.out.print(isCircular(head)? ````"Yes "``` `: ` `                    ````"No "``` `); ` ` `  `    ``// Making linked list circular ` `    ``head.next.next.next.next = head; ` ` `  `    ``System.out.print(isCircular(head)? ````"Yes "``` `: ` `                    ````"No "``` `); ` ` `  `} ` `} ` ` `  `// This code contributed by Arnab Kundu `

## C#

// C# program to check if
using System;
public class GFG
{

public class Node
{
public int data;
public 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.
static Node newNode(int data)
{
Node temp = new Node();
temp.data = data;
temp.next = null;
return temp;
}

/* Driver code*/
public static void Main(String []args)
{

“No ” );

“No ” );

}
}
// This code has been contributed by 29AjayKumar

Output :

```No
Yes```

code