Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Last Updated: 16 Dec, 2020

Easy

```
The first line of input contains a single integer T, representing the number of test cases or queries to be run.
The first line of each test case contains the elements of the circular linked list separated by a single space and terminated by -1. Hence, -1 would never be a list element.
The second line contains a boolean value ‘val’, if ‘val’ is 1 then the given list is circular.
```

```
For each test case, print “True” if the given linked list is circular, else print “False”.
Print the output of each test case in a separate line.
```

```
You do not need to print anything, it has already been taken care of. Just implement the given function.
```

```
1 <= T <= 10
1 <= N <= 5 * 10^4
-10^9 <= data <= 10^9 and data != -1
0 <= val <= 1
Where 'N' is the number of nodes in the linked list, ‘data’ represents the value of the nodes of the list.
Time Limit: 1 sec
```

In this approach, we will traverse the list until we reach the base case.

Base case: When we reach one of the following node

- NULL: represents that the given linked list is not circular.
- head: represents that the given linked list is circular.

Recursive state: circularLLHelper(cur, head)

Next recursive state: circularLLHelper(cur->next, head)

The idea is to store the head node and traverse the list. If we reach the end (NULL), then the given linked list is not circular, else if we reach the head again, then the linked list is circular.

**Algorithm**

Take a pointer *‘cur’*, which initially points to the head node. Move the pointer ‘cur’ to the next node until you find one of the following nodes

- NULL: represents that the given linked list is not circular.
- head: represents that the given linked list is circular.

Similar problems

Deletion In Doubly Linked List

Easy

Posted: 22 Apr, 2022

Deletion In Doubly Linked List

Easy

Posted: 22 Apr, 2022

Insertion In Doubly Linked List

Easy

Posted: 24 Apr, 2022

Insertion In Doubly Linked List

Easy

Posted: 24 Apr, 2022

LRU Cache

Moderate

Posted: 10 Sep, 2022

Delete Nodes On Regular Intervals

Ninja

Posted: 19 Dec, 2022

Add One To Linked List

Hard

Posted: 19 Dec, 2022