CJCoding With Joseph
15free / daySign inCreate account

Find a Node by Index and by Value

A linked list has no random access, so both lookups are the same walk with a different stopping rule: one counts steps, the other compares values.

Requirements:
1. getAt(int index) returns the value at that 0-based position, or -1 when the index is negative or past the end.
2. indexOf(int value) returns the 0-based position of the first node holding value, or -1 when no node holds it.

The Node class is provided - do not rename it and do not add public to it, because a Java source file may contain only one public class.

Do NOT write a main method.

Expected Output:

10
30
-1
2
-1
10 -> 20 -> 30 -> 40 -> null

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Data Structures & AlgorithmsLinked Lists
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (sysout, psvm, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.