People's QA checks sometimes model workflow transitions as a linked sequence of states. Given an array representation of those transitions, determine whether the sequence starting at head eventually loops back to a previously visited state.
Each array index represents one state, and next_indices[i] gives the index of the next state. A value of -1 indicates the end of the sequence. Return True if a cycle exists and False otherwise. Solve the problem with O(1) additional space using two pointers moving at different speeds.
Implement detect_cycle(next_indices, head).
next_indices, a list of integers, and head, an integer index. Each pointer is either -1 or a valid index in next_indices.def detect_cycle(next_indices, head):