Linked List · Easy

251. Check if a Linked List Is Circular

Topics: Linked List

Solve it on DeVert Campus - free code runner

Problem

A singly linked list has n node values. Its last node's next pointer is connected back to the node at 0-indexed position pos (or there is no connection at all if pos is -1). The list is a proper circular linked list only if the last node connects all the way back to the very first node (pos = 0), so that every node lies on a single cycle. Print 'Yes' if it is a circular linked list this way, otherwise print 'No'.

Input and output

Input: The first line contains an integer n. The second line contains n space-separated integers, the list's values. The third line contains pos, the 0-indexed position the last node's next pointer connects back to, or -1 for no connection. Output: Print 'Yes' if the list is a proper circular linked list (the last node connects back to the very first node), otherwise print 'No'.

Example

Input:
4
1 2 3 4
0
Output:
Yes

Constraints

1 <= n <= 10^5, -1 <= pos < n

Key observation

This is a STRICTER condition than simply 'has a loop' - a list can have a loop (detectable by Floyd's algorithm) without being properly circular, if the loop doesn't happen to include the very first node.

Intuition

Check whether a linked list is a PROPER circular list, meaning the last node connects all the way back to the very FIRST node. Brute force: Walk the entire list from the head, counting nodes visited, and check if you eventually return to the head after visiting EXACTLY n nodes - this works but requires a full traversal, when checking just the last node's destination directly is enough. Optimised: Since a proper circular list is defined SPECIFICALLY by the last node connecting back to the head, checking that ONE single pointer's destination directly answers the question in O(1) extra work - no need to walk the whole list first, since ANY other connection point would immediately disqualify it.

Traversal with visited marking

Build the linked list as described, then traverse it starting from the head, marking each visited node in a hash set/array and counting steps. Keep following next pointers until either a null pointer is hit (not circular) or a previously visited node is reached; check that the revisited node is exactly the head and that exactly n distinct nodes were visited beforehand. This is safe and general but spends extra memory tracking visited nodes that isn't strictly necessary here. Time: O(n) Space: O(n)

Plain pointer traversal

Traverse the constructed list from the head using only a running counter and a single pointer, following next links until a null is hit (answer No) or until the pointer returns to some already-known node. Compare that returned-to node against the head by reference and also check that the counter reached n before the return - only if both hold is the list a genuine full circular list starting and ending at node 0. This drops the extra visited-set memory of the brute approach, needing only O(1) bookkeeping while still walking the whole list once. Time: O(n) Space: O(1)

Direct check on pos

Because the problem already tells us exactly where the last node's next pointer goes (the given index pos), no traversal of any list is required at all: the list is a proper circular list covering every node if and only if pos equals 0, since that is precisely the condition under which the last node reconnects to the very first node and every node lies on one single cycle. Simply read n and pos and print 'Yes' if pos == 0, otherwise 'No'. This answers the question in constant time using only the input values already provided. Time: O(1) Space: O(1)

Step by step

1. Check where the LAST node's next pointer leads. 2. If it leads back to the very FIRST node (position 0), the list is a proper circular list. 3. If it leads anywhere else (including nowhere, pos = -1), it is NOT a circular list - some nodes near the head would be left outside any cycle.

Complexity

Time: O(1) if the last node is directly known/reachable, or O(n) if you must first walk to find the last node; either way, this is far cheaper than a full cycle-analysis algorithm. Space: O(1) - no extra data structure needed.

Edge cases

pos = 0 (a proper circular list, answer Yes), pos = -1 (no connection at all), and pos pointing to some middle node (a loop exists but excludes earlier nodes, answer No).

Interview tip

Be ready to clearly distinguish 'circular linked list' (last node connects to the FIRST node specifically) from the more general 'has a loop' (last node connects to ANY earlier node) - this problem is testing that precise distinction.

All DSA problems