Linked List Cycle
EasyLinked Lists
Given an array of values and an integer pos, build a linked list where the tail connects back to the node at index pos (0-indexed). If pos is -1, there is no cycle. Return true if the linked list has a cycle, false otherwise.
Example:
Input: values = [3,2,0,-4], pos = 1
Output: true
Explanation: Tail connects to node at index 1 (value 2)
Input: values = [1], pos = -1
Output: false
Explanation: No cycle
Note: Your function receives values (array) and pos (number). Build the linked list internally and detect the cycle.
Constraints:
- •
0 <= values.length <= 10^4 - •
-10^5 <= values[i] <= 10^5 - •
-1 <= pos < values.length
Linked List Cycle
EasyLinked Lists
Given an array of values and an integer pos, build a linked list where the tail connects back to the node at index pos (0-indexed). If pos is -1, there is no cycle. Return true if the linked list has a cycle, false otherwise.
Example:
Input: values = [3,2,0,-4], pos = 1
Output: true
Explanation: Tail connects to node at index 1 (value 2)
Input: values = [1], pos = -1
Output: false
Explanation: No cycle
Note: Your function receives values (array) and pos (number). Build the linked list internally and detect the cycle.
Constraints:
- •
0 <= values.length <= 10^4 - •
-10^5 <= values[i] <= 10^5 - •
-1 <= pos < values.length
Press Run or Ctrl+Enter to execute