← Back to Data Structures & Algorithms
Question 445
Insert and Remove at a Position
Insert and Remove at a Position
Both methods share one invariant: to change a link you must be standing on the node BEFORE the target, because a singly linked node cannot see backwards. Requirements: 1.insertAt(int index, int value)puts a new node at that 0-based position and returns true; it returns false and changes nothing when index is negative or greater thansize(). 2.removeAt(int index)unlinks the node at that position and returns its value, or -1 when index is negative or not less thansize(). 3. Index 0 means the head in both methods. 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:
true 10 -> 15 -> 20 -> 30 -> null true 10 -> 15 -> 20 -> 30 -> 40 -> null false 5
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
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.