Find Linked List Tail
You are given a linked list represented using two arrays: next_node and node_value. The next_node array stores the index of the next node in the list for each node. For example, if next_node[i] = j, it means node i points to node j. The last node in the list has a special value in next_node, which is -1. The node_value array simply stores the values associated with each node, but these values are not used in this problem. You are also given the index of the head node. Your task is to find and print the value of the last node (tail) in the linked list.
Input
The first line contains an integer N, the number of nodes. The second line contains N space-separated integers representing the next_node array. The third line contains N space-separated integers representing the node_value array. The fourth line contains an integer, the index of the head node.
Output
Print a single integer, the value of the tail node.
Constraints
1 <= N <= 1000
Each element in next_node is an integer between -1 and N-1.
Each element in node_value is an integer between -10^9 and 10^9.
The next_node array represents a valid singly linked list starting from the head node, with exactly one node having -1 as its next pointer.
The head node index is between 0 and N-1.
Example 1
5 1 2 3 4 -1 10 20 30 40 50 0
50
The list starts at index 0. 0 -> 1 -> 2 -> 3 -> 4. Node 4 is the tail with value 50.
Example 2
3 -1 0 1 100 200 300 0
100
The list starts at index 0. 0 is the tail node itself with value 100.
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 -1 10 20 30 40 50 0
3 -1 0 1 100 200 300 0