Insert After a Linked-List Node
Problem statement
A singly linked list is represented by values. Insert newValue immediately after the node at zero-based position, preserve every existing link, and return the resulting values.
The position always names an existing node.
Function
insertAfter(values: int[], position: int, newValue: int) → int[]Examples
Example 1
values = [4,7,9]position = 1newValue = 8return = [4,7,8,9]The new node is linked between 7 and 9.
Example 2
values = [5]position = 0newValue = 6return = [5,6]Insertion after the tail creates a new tail.
Constraints
1 <= values.length <= 100000.0 <= position < values.length.- All values fit signed 32-bit integers.
Source note: Author-written prompt excerpt retained for source fidelity.