#K80222. Search in Linked List

    ID: 35483 Type: Default 1000ms 256MiB

Search in Linked List

Search in Linked List

You are given a singly-linked list and a target value. Your task is to find the first occurrence of the target in the list and return its position (using 1-indexing). If the target value is not found in the list, print -1.

The linked list is represented by an integer n (the number of nodes), followed by n space-separated integers (the values of the nodes), and finally an integer representing the target value. For example, given the input:

5
1 3 5 7 9
5

The answer would be 3 because the target value 5 is found at the 3rd position in the list.

If a formula is needed, for instance, the index can be calculated as follows: $$index = \min\{ i \mid node_i = target \}$$, where \( node_i \) denotes the value of the i-th node in the list.

inputFormat

The input is read from stdin and is structured as follows:

  • The first line contains an integer n, representing the number of nodes in the linked list.
  • The second line contains n space-separated integers representing the values of the nodes in order.
  • The third line contains a single integer representing the target value to search for.

outputFormat

Output the 1-indexed position of the first occurrence of the target value. If the target is not present in the list, output -1. The result should be printed to stdout.

## sample
5
1 3 5 7 9
5
3

</p>