Insert into an Unrolled Linked List
Problem statement
An unrolled linked list is represented by nodes, where every row stores one node's active integer values and has length at most nodeCapacity. Concatenating the rows gives the logical list.
Insert value before logical position index, where index may equal the total size.
- If the selected node has space, insert in that node.
- If insertion occurs strictly inside a full node, keep the prefix and new value in that node and move the suffix into one newly inserted following node.
- An index at a node boundary belongs to the following node when one exists.
- Appending after a full final node creates a one-value node.
Return all node rows after insertion.
Function
insertUnrolled(nodes: int[][], nodeCapacity: int, index: int, value: int) → int[][]Examples
Example 1
nodes = [[9,6,7,4,10]]nodeCapacity = 5index = 2value = 8return = [[9,6,8],[7,4,10]]The full node splits at the insertion point: its prefix and 8 stay first, and the old suffix moves to the new node.
Example 2
nodes = [[1,2],[3]]nodeCapacity = 3index = 2value = 9return = [[1,2],[9,3]]The boundary index belongs to the next node, which has capacity.
Constraints
1 <= nodes.length <= 100000.1 <= nodes[i].length <= nodeCapacity <= 1000.0 <= index <= total number of stored values.- All values fit signed 32-bit integers.