Check if a linked list is Circular Linked List - GeeksforGeeks
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.
Read full article from Check if a linked list is Circular Linked List - GeeksforGeeks
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.
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.
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);
}