Find Path End
You are given a representation of a linked list using an array. The array next_pointers of size N is provided, where next_pointers[i] stores the index of the next element in the list after element i. The list starts at index 0. Your task is to find the index of the last element in this linked list. The list is guaranteed to be non-empty and will eventually end (i.e., it does not contain cycles or run out of bounds).
Input
The first line contains an integer N (2 <= N <= 1000). The second line contains N space-separated integers representing the next_pointers array. next_pointers[i] will be between 0 and N-1, inclusive.
Output
Print a single integer: the index of the last element in the linked list.
Constraints
2 <= N <= 1000. All values in next_pointers are valid indices within the range [0, N-1]. The structure represents a valid linked list starting from index 0 and guaranteed to end without cycles.
Example 1
5 1 2 3 4 4
4
The list starts at 0 -> 1 -> 2 -> 3 -> 4. Node 4 points to itself, so 4 is the end.
Example 2
3 1 2 2
2
The list starts at 0 -> 1 -> 2. Node 2 points to itself, so 2 is the end.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
5 1 2 3 4 4
3 1 2 2