Check If Circular Linked List | Practice | GeeksforGeeks

Given head, the head of 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. An empty linked list is considered as circular.
Note: The linked list does not contains any inner loop.
Example 1:
Input:
LinkedList: 1->2->3->4->5
(the first and last node is connected,
i.e. 5 --> 1)
Output: 1

Example 2:
Input:
LinkedList: 2->4->6->7->5->1
Output: 0
Your Task:
The task is to complete the function isCircular() which checks if the given linked list is circular or not. It should return true or false accordingly. (the driver code prints 1 if the returned values is true, otherwise 0)
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Constraints:
1 <=Number of nodes<= 100
0
navins2 days ago
Python Code: 0.02sec
def isCircular(head):   # Code here   temp=head.next   if temp is None:       return 0   if head is None :       return 1      while (temp):       if temp.next is None:           return 0       elif temp==head:           return 1       else:           temp=temp.next   return 1
0
sudeepgarg6716 days ago
Node *temp = head;  while(temp->next!=NULL){      if(temp->next==head){          return true;      }      temp = temp->next;  }  return false;
+1
prateek_jakhar1 week ago
JAVA SOLUTION
 		Node temp = head;
	    while(temp.next!=null){
	        temp = temp.next;
	        if(temp==head || temp.next==head) break;
	    }
	    if(temp.next==null) return false;
	    return true;
0
gurankitbehal781141 week ago
bool isCircular(struct Node *head){   if(head==NULL){       return true;   }   struct Node*p=head;   while(1){       if(p->next==head){           return true;       }       if(p->next==NULL){           return false;       }       p=p->next;   }   }
0
kumaramresh5941 week ago
if(head==NULL){      return true;  }  Node *curr = head;   while(curr->next!=NULL){       if(curr->next==head){           return true;       }       if(curr->next==NULL){           return false;       }       curr=curr->next;   }
0
alokkumar90192 weeks ago
bool isCircular(Node *head)
{
  // Your code here
      Node *curr=head;
      while(curr!=NULL){
          if(curr->next==head){
              return true;
          }
          if(curr->next==NULL){
              return false;
          }
          curr=curr->next;
      }
      
}
+1
shahabuddinbravo402 weeks ago
bool isCircular(Node *head){  if(head==NULL){      return true;  }  else{      Node *ptr=head->next;            while(true){          if(ptr==head){              return true;          }                    if(ptr==NULL){              return false;          }                    ptr=ptr->next;      }  }} 
0
sudhakarsinghbaghel1233212 weeks ago
bool isCircular(Node *head)
{
   // Your code here
   Node* t=head;
   do{
       t=t->next;
   }
   while(t!=head and t!=NULL);
    if(t==head)
    return true;
    else
    return false;
    
}
0
hharshit81183 weeks ago
bool isCircular(Node *head){  Node *slow = head, *fast = head;  while(fast && fast->next){      slow = slow->next;      fast = fast->next->next;      if(slow == fast){          return true;      }  }  if(fast == NULL){      return false;  }}
0
sohalkumarsingh3 weeks ago
JAVA Solution -
boolean isCircular(Node head) {    if (head == null || head.next == head) {        return true;    }         Node currNode = head.next;    while (currNode != null) {        if (currNode == head) {            return true;        }        currNode = currNode.next;    }        return false;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.