Dodomeki
Sign in

23. Cycle by Position

Easy

You are given the values of a list in an array valores and a position pos. Imagine that the next pointer of the last node points to valores[pos]; when pos is -1, the last node ends at null. Return true if this representation forms a cycle.

Example 1
Input: valores = [6, 1, 9, 4], pos = 1
Output: true
Example 2
Input: valores = [2, 5, 8], pos = -1
Output: false
Constraints
  • 0 <= valores.length <= 100000
  • pos é -1 ou um índice válido de valores.
Linked list
Caso 1
valores =
[6,1,9,4]
pos =
1
Expected =
true
Caso 2
valores =
[2,5,8]
pos =
-1
Expected =
false

Run checks these 2 cases. Submit runs all 6, including hidden ones.