This practice workspace is built for desktop. Open it on a larger screen to start solving.
Linked List Cycle
Given the head of a linked list, determine whether it contains a cycle. A cycle exists if some node in the list can be reached again by continuously following the next pointer.
Input/output convention for this exercise: for portability, the list is passed as an array of values plus a pos integer indicating the index where the tail's next reconnects (pos = -1 means no cycle). The provided starter includes ListNode, arrayToList, listToArray, and a _wireCycle(head, pos) helper that wires up the cycle for you. Convert the input to a real linked list, wire the cycle, then run your detection.
The classic solution is Floyd's tortoise and hare: advance two pointers at different speeds; if they ever meet, the list has a cycle.
Examples
Constraints
- 0 <= values.length <= 10^4
- -10^5 <= values[i] <= 10^5
- pos is -1 or a valid index in [0, values.length).
Preview Mode
This is a read-only preview of DSAMind's AI coaching. Sign up to get custom feedback on your own solution.
Your approach
- Pattern:Linked List
- Time:O(N)
- Space:O(1)
Complete approach — pattern and complexity both named.
Coach
What you got right
You correctly matched the sorted input requirement and implemented a linear-time scan using two pointers.
Where it diverged
No divergence detected. The code correctly aligns with the stated approach.
Next attempt: focus on
Try solving related sliding window problems to build familiarity with two-pointer variants.
Signals
No signals fired — clean run, you stayed in flow.