One possible solution is to keep a list for all visited positions.

If we hit a position that is already in the list, we've detected a cycle.

For this solution, time complexity is O(n) and space complexity is O(n).

How to do this in O(n) time and O(1) space?

The catch is to realize that if there is a cycle, it will result in an infinite loop