Write a program to check linkedlist is circular or not.

Given a linkedlist, we have to check a linkedlist is circular or not.

Example:

Input:1 2 3 4 5 6 1
Output:true

Logic:

STEP 1:start traversing a linkedlist till reach its end .
STEP 2:if last node is pointing to first node of linkedlist
STEP 3:then it is circular otherwise or not.

Code

bool CheckCircular(struct Node *head) 
{ 
    if (head == NULL) 
    {
       return true; 
  
    }
    struct Node *temp = head->next; //first node
    while (temp!= NULL&&temp != head) //checking till you reach end 
    {
       node = node->next; 
    }
   if(temp==head)
   {
       return true;
   }
   else
   {
       return false;
   }
} 

Ouput:

true

Time Complexity:O(N)

Space Complexity:O(1)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top