A PlayStation Network navigation history is represented as a singly linked structure. Each item points to the index of the next item, or -1 when navigation ends. Given the current head index, determine whether the reachable structure contains a cycle and return the index where that cycle begins.
Use O(1) additional space. Do not modify the input.
Implement detect_cycle(next_indices, head_index).
next_indices is a list of integers. For item i, next_indices[i] is the index of its next item, or -1.head_index is the index of the current head, or -1 for an empty structure.-1 if no cycle exists.head_index are irrelevant.def detect_cycle(next_indices, head_index):