123. Search a Linked List
Foundation1000 ms256 MBSolved by 0%
Build the list, then read a value k on the next line.
Print the 0-based position where k first appears, or -1 if it is not in the list.
Constraints - `0 ≤ n ≤ 100000` - `-1000000 ≤ value, k ≤ 1000000`
Input
The first line contains an integer n, the number of nodes.
The second line contains n space-separated integers, the node values from head to tail.
The starter code builds the list for you; work on the nodes, not the array.
The third line contains an integer k.
Output
Print one integer, the 0-based position where k first appears, or -1 if it is absent.
Input5
4 8 15 16 23
15
Output2
Notefinds a value in the middle
Input4
1 2 3 4
9
Output-1
Notesearches for a value that is absent
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.
Hint 1Where to start
Hint 2The approachOpen hint 1 first. This one carries on from it.
Hint 3PseudocodeOpen hint 2 first. This one carries on from it.
Hint 4Full solutionOpen hint 3 first. This one carries on from it.