Circularly Linked
You are given the head of a linked list containing integers, You need to find out whether the given linked list is circular or not.
Note :
1. A linked list is said to be circular if it has no node having its next pointer equal to NULL and all the nodes form a circle i.e. the next pointer of last node points to the first node.
2. An empty linked will also be considered as circular.
3. All the integers in the linked list are unique.
4. In the input, the next pointer of a node with i’th integer is linked to the node with data (i+1)’th integer (If (i+1)’th node exists). If there is no such (i+1)’th integer then the next pointer of such node is set to NULL.
Input format :
The first line of input contains an integer T, denoting the number of test cases.
The first line of each test case consists of an integer N, denoting the number of links in the linked list.
The second line of each test case consists of N space-separated integers denoting the data of the linked list and linking between nodes of the linked list as described in the notes above.
For more clarity please refer to the sample input.
Output format :
For each test case, print ‘True’ or ‘False’ depending on whether the linked list is circular or not, in a separate line.
Note :
You don't have to print anything, it has already been taken care of. Just Implement the given function.
Constraints :
1 <= T <= 10 ^ 2
1 <= D <= 10 ^ 6, here D is data stored in the node.
0 <= N <= 10 ^ 4
Time Limit: 1 sec
AnswerBot
1y
The task is to determine whether a given linked list is circular or not.
A linked list is circular if the next pointer of the last node points to the first node.
An empty linked list is also considered ...read more
CodingNinjas
author
2y
Optimal Solution
- Take two Node pointers and initialise them to head of the linked list.
- Iterate through the linked list as follows:
- In each iteration move the slow pointer by one node and fast by two nod...read more
Help your peers!
Add answer anonymously...
Top Decimal Point Analytics Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
>
Decimal Point Analytics Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app