A Capgemini Engineering service represents a singly linked list using an array of next-pointer indices. Given this representation, determine whether traversing from a specified head node eventually revisits a node.
Implement has_cycle(next_index, head). next_index[i] is the index of the node reached from node i, or -1 if the node has no successor. The list may contain nodes that are unreachable from head; ignore them. Return True if the reachable portion contains a cycle, otherwise return False.
Use O(1) auxiliary space. Do not modify the input. An optimal solution should run in linear time relative to the number of reachable nodes.
next_index, a list of integers, and head, an integer node index.head contains a cycle.-1 represents the null pointer.def has_cycle(next_index, head):