  Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email  Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Last Updated on November 28, 2022 by Prepbytes As we already saw many articles over the linked list. Let’s have a look at another article on the linked list in which we have to check linked list is circular or not. A linked list is a linear data structure in which data is not stored continuously just like arrays.

## How to check linked list is circular or not

Given a singly linked list, check if it is circular or not.

#### Examples

Input: Output: NO

Input: Output: YES

Input: Output: NO

Before checking for a circular linked list, do you know what is a circular linked-list?

A linked list is called circular if its last node points back to its first node.

Now we will be able to understand the examples given above. Here the last node points to NULL, hence it is not a circular linked-list. Here the last node points back to the first node, hence it is a circular linked-list. Here, the last node points don’t point back to the first node. So, it is not a circular linked list.

I hope you got the problem, now before moving to the approach section, try to think how will you approach it?

## Approach 1 to check linked list is circular or not

One simple approach is to store the head of the linked list and now start traversing the linked list from next node of head and check if:

• If we reached null, or we got stuck in a cycle that doesn’t include head node, it means that the linked list is not a circular linked list.

This approach is pretty simple, but here we will see how using cycle detection technique we find out if a linked list is circular or not in the next approach.

## Approach 2 to check linked list is circular or not

I hope you got an idea of what a circular linked list is.

Let’s now look at how to identify such a linked list.

• One thing we can observe is that only the linked lists containing a cycle can be a circular linked-list. So, if a linked list doesn’t have any cycle, we know that it is not circular.
• Another observation is that circular linked-lists have a cycle, and the cycle starts at the first node itself.
• So, finally, a linked list with a cycle will be called a circular linked list only if the point at which the cycle starts is the first node.

To identify a linked list as a circular linked list, we need to make the following checks:

• If the linked list contains a cycle or not.
• If it has a cycle, then whether the node at which the cycle starts is the head node or not.

Let’s state this in another way:

• A linked list is called circular if the next pointer of the last node of the list points back to the first node. If this pointer points to NULL or any other previous nodes (other than the first node), then the linked list won’t be called circular.

Since it is clear what we need to do, take some time and think about how to implement it.

You must know how to detect a cycle in a linked list before proceeding further.

Below is the algorithm explaining the steps we need to take to implement our idea.

## Algorithm to check linked list is circular or not

• Detect a cycle in the given linked list (we will use Floyd’s cycle detection algorithm).
• If no cycle is found, then the linked list is linear. So return false.
• Else, if the cycle is found, find the starting point of the cycle.
• If the first node is the starting point, then the linked list is circular and return true.
• Else return false.

### Dry Run to check linked list is circular or not ## Code Implementation to check linked list is circular or not :

```#include <stdio.h>
#include<stdbool.h>
#include<stdlib.h>

struct Node {
int data;
struct Node* prev;
struct Node* next;
};

void push(struct Node** head_ref, int new_c)
{
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));
new_node->data = new_c;
new_node->prev = NULL;
}

while(temp!=NULL)
{ //if temp points to head then it has completed a circle,thus a circular linked list.
return true;
temp=temp->next;
}

return false;

}
int main(void)
{

printf("Yes\n");
else
printf("No\n");

}

```
```
#include<bits stdc++.h="">
using namespace std;

struct Node {
int val;
Node* next;

Node(int value){
val = value;
next = NULL;
}
};

Node* new_node = new Node(new_val);
}

// detect cycle
Node *slow, *fast;

while(fast!=NULL && fast->next!=NULL){
slow = slow->next;
fast = (fast->next)->next;

if(slow==fast) break;
}

if(fast==NULL) return false;
if(fast->next == NULL) return false;

// now we have a loop
// find its starting point
while(slow!=fast){
slow = slow->next;
fast = fast->next;
}

// check if starting point of loop
// is the first node
else return false;
}

int main(){
Node* h1 = NULL;

push_front(&h1, 5);
push_front(&h1, 4);
push_front(&h1, 3);
push_front(&h1, 2);
push_front(&h1, 1);

// let's check if our lined list if circular or not
if(isCircular(h1)) cout<<"YES\n";
else cout<<"NO\n";

// make the next pointer of last node point to first node
Node* i = h1;
while(i->next!=NULL){
i = i->next;
}

i->next = h1;

// 1->2->3->4->5
//  \----------/

// let's check if our function finds out or not
if(isCircular(h1)) cout<<"YES\n";
else cout<<"NO\n";

// make the next pointer of last node point to second node
i->next = (h1->next);

// now the linked list isn't circular
// but has a loop
// 1->2->3->4->5
//    \--------/

// let's check if our function finds out or not
if(isCircular(h1)) cout<<"YES\n";
else cout<<"NO\n";
}

```
```

class Circular
{

static class Node
{
int data;
Node next;
}

{

return true;

while (node != null && node != head)
node = node.next;

}

static Node newNode(int data)
{
Node temp = new Node();
temp.data = data;
temp.next = null;
return temp;
}
public static void main(String args[])
{

}
}
```
```
# Node structure of a doubly linked list node
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None

# Using this function we will be inserting a new node in the list

new_node = Node(data)
new_node.data = data

new_node.next = new_node
new_node.prev = new_node

else :

new_node.prev = last
last.next = new_node

# Using this function we will be merging two sorted doubly linked list
# merge2SortedDLL stands for merge two sorted doubly linked list
def merge(first, second):

if (first == None):
return second

if (second == None):
return first

if (first.data < second.data) :
first.next = merge(first.next,
second)
first.next.prev = first
first.prev = None
return first

else :
second.next = merge(first,
second.next)
second.next.prev = second
second.prev = None
return second

# Using this function we will be merging two sorted doubly circular linked list
# merge2SortedDCLL stands for merge two sorted doubly circular linked list

else:

# Using this function we will be printing the linked list content

print(temp.data, end = " ")
temp = temp.next

print(temp.data, end = " ")

if __name__=='__main__':

print("Original linked list 1: ", end = "")
print()

print("Original linked list 2: ", end = "")
print()

print("Final Sorted List: ", end = "")
```
``````Output
NO
YES
NO``````

Time complexity of check linked list is circular or not: O(n), where n is the number of nodes in the linked list.

Conclusion

Through this article, we learned how to check linked list is circular or not. Problems like these are good for strengthening your concepts in LinkedList. You can check our questions bank on Linked List, which is curated by our expert mentors at PrepBytes, you can follow this link Linked List.

## FAQs

1. Can we access the random element of the LinkedList?
We can not access any element of the LinkedList directly. We don’t have direct access to every element of LinkedList. If we want to access the ith element of the LinkedList, then we need to traverse the LinkedList till the ith index.

2. Can a linked list be circular?
Yes, the linked list can be a circular linked list if the last node is connected to the first node of the linked list and forms a circle.

3. What are the four types of linked lists?
1. Ankit says: