Insert a Doubly Linked List Node at a Position
Problem statement
Given the values of a doubly linked list, a zero-based position, and a new value, insert a node before the current node at that position. Position values.length appends at the tail. Return the values in forward order.
For the practice runner, values serializes the list from head to tail. Build or reason about the doubly linked structure, connect both directions around the new node, update the head when needed, and return the serialized result.
Function
insertAtPosition(values: int[], position: int, value: int) → int[]Examples
Example 1
values = [1,3,4]position = 1value = 2return = [1,2,3,4]The new node is connected between 1 and 3.
Example 2
values = []position = 0value = 7return = [7]Inserting into an empty list creates its head and tail.
Constraints
0 <= values.length <= 10^5.-10^9 <= values[i], value <= 10^9.0 <= position <= values.length.