Circular Linked List Detection
You are provided with the head of a linked list containing integers. Your task is to determine if the linked list is circular.
Note:
- A linked list is considered circular if it contains no node having a next pointer equal to NULL, and all nodes form a closed loop, meaning the next pointer of the last node points back to the first node.
- An empty linked list is also considered circular.
- All integers in the linked list are unique.
- In the input, the next pointer of a node with the i-th integer is linked to the node with the data of the (i+1)-th integer, if such a node exists. If no (i+1)-th integer exists, the next pointer is set to NULL.
Input:
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 representing the data of the linked list and their connections as per the description above.
Output:
For each test case, output 'True' or 'False' indicating if the linked list is circular.
Example:
Input:
T = 2
3
1 2 3
1
1
Output:
False
True
Constraints:
- 1 ≤ T ≤ 102
- 1 ≤ D ≤ 106, where D is the data stored in a node
- 0 ≤ N ≤ 104
- 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
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