Given a singly linked list, find if the linked list is circular or not. A linked list is called circular if it is not NULL-terminated and all nodes are connected in the form of a cycle. Below is an example of a 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.
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<bits/stdc++.h>
usingnamespacestd;
/* Link list Node */
structNode
{
intdata;
structNode* next;
};
/* This function returns true if given linked
list is circular, else false. */
boolisCircular(structNode *head)
{
// An empty linked list is circular
if(head == NULL)
returntrue;
// Next of head
structNode *node = head->next;
// This loop would stop 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(intdata)
{
structNode *temp = newNode;
temp->data = data;
temp->next = NULL;
returntemp;
}
/* Driver program to test above function*/
intmain()
{
/* Start with the empty list */
structNode* head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(3);
head->next->next->next = newNode(4);
isCircular(head)? cout << "Yes\n":
cout << "No\n";
// Making linked list circular
head->next->next->next->next = head;
isCircular(head)? cout << "Yes\n":
cout << "No\n";
return0;
}
Java
// Java program to check if
// linked list is circular
importjava.util.*;
classGFG
{
/* Link list Node */
staticclassNode
{
intdata;
Node next;
}
/*This function returns true if given linked
list is circular, else false. */
staticbooleanisCircular( Node head)
{
// An empty linked list is circular
if(head == null)
returntrue;
// Next of head
Node node = head.next;
// This loop would stop 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.
staticNode newNode(intdata)
{
Node temp = newNode();
temp.data = data;
temp.next = null;
returntemp;
}
/* Driver code*/
publicstaticvoidmain(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\n":
"No\n");
// Making linked list circular
head.next.next.next.next = head;
System.out.print(isCircular(head)? "Yes\n":
"No\n");
}
}
// This code contributed by Arnab Kundu
Python3
# A simple Python program to check if a linked list is circular
# Node class
classNode:
# Function to initialise the node object
def__init__(self, data):
self.data =data # Assign data
self.next=None# Initialize next as null
# Linked List class contains a Node object
classLinkedList:
# Function to initialize head
def__init__(self):
self.head =None
defCircular(head):
ifhead==None:
returnTrue
# Next of head
node =head.next
i =0
# This loop would stop in both cases (1) If
# Circular (2) Not circular
while((node isnotNone) and(node isnothead)):
i =i +1
node =node.next
return(node==head)
# Code execution starts here
if__name__=='__main__':
llist =LinkedList()
llist.head =Node(1)
second =Node(2)
third =Node(3)
fourth =Node(4)
llist.head.next=second;
second.next=third;
third.next=fourth
if(Circular(llist.head)):
print('Yes')
else:
print('No')
fourth.next=llist.head
if(Circular(llist.head)):
print('Yes')
else:
print('No')
# This code is contributed by Sanket Badhe
C#
// C# program to check if
// linked list is circular
usingSystem;
publicclassGFG
{
/* Link list Node */
publicclassNode
{
publicintdata;
publicNode next;
}
/*This function returns true if given linked
list is circular, else false. */
staticboolisCircular( Node head)
{
// An empty linked list is circular
if(head == null)
returntrue;
// Next of head
Node node = head.next;
// This loop would stop 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.
staticNode newNode(intdata)
{
Node temp = newNode();
temp.data = data;
temp.next = null;
returntemp;
}
/* Driver code*/
publicstaticvoidMain(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);
Console.Write(isCircular(head)? "Yes\n":
"No\n");
// Making linked list circular
head.next.next.next.next = head;
Console.Write(isCircular(head)? "Yes\n":
"No\n");
}
}
// This code has been contributed by 29AjayKumar
Javascript
<script>
// javascript program to check if
// linked list is circular
/* Link list Node */
class Node {
constructor(val) {
this.data = val;
this.next = null;
}
}
/*
* This function returns true if given linked list is circular, else false.
// C program to check if the linked list is circular
#include <stdio.h>
#include <stdlib.h>
structNode {
intdata;
structNode* next;
};
intisCircular(structNode* head)
{
// If linked list is empty it is circular
if(head == NULL)
return1;
structNode* ptr;
ptr = head->next;
// Traversing linked list till last node
while(ptr != NULL && ptr != head)
ptr = ptr->next;
// Condition for circular linked list
return(ptr == head);
}
// Function to create new Node
structNode* newnode(intdata)
{
structNode* temp;
temp = (structNode*)malloc(sizeof(structNode));
temp->data = data;
temp->next = NULL;
returntemp;
}
intmain()
{
// code
// Starting with empty list
structNode* head = newnode(1);
head->next = newnode(2);
head->next->next = newnode(3);
head->next->next->next = newnode(4);
// Checking for circular list
if(isCircular(head))
printf("Yes\n");
else
printf("No\n");
// If not circular making it circular
head->next->next->next->next = head;
if(isCircular(head))
printf("Yes\n");
else
printf("No\n");
return0;
}
Output
No
Yes
Time Complexity: O(n), As we have to move through the list at least one time whether it is circular or not. Auxiliary Space: O(1), As constant extra space is used.
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 review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy