📘
Linked List Cycle
EasyLinked List
Given a list of values and an integer pos indicating where the tail connects to (0-indexed), determine if the linked list has a cycle.
pos is -1 if there is no cycle. Output True if there is a cycle, False otherwise.
Example:
Input:
3,2,0,-4 1
Output:
True
Reasoning:
- The input list of values is used to create a linked list: 3 -> 2 -> 0 -> -4.
- The integer
posis 1, meaning the tail of the list connects to the node at index 1 (0-indexed), which has a value of 2. - The connection creates a cycle: 3 -> 2 -> 0 -> -4 -> 2, since the last node (-4) points back to the node with value 2.
- Because a cycle is detected in the linked list, the output is
True.
Constraints:
- 0 <= number of nodes <= 10^4
- -10^5 <= Node.val <= 10^5
- pos is -1 or a valid index
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.