Please read the instructions carefully before starting the exam.
Once you start the exam, the timer will begin, and you cannot pause it.
Ensure that you complete and submit your code within the given time if the timer is enabled.
In a smart shopping assistant app, users add items to their shopping list as they think of them.
Later, they may want to rearrange their list based on item priority.
The shopping list is stored as a singly linked list.
Your task is to move the item at position fromPosition
to a new position toPosition
within the list.
This rearrangement must be done in-place by manipulating pointers — not by copying items into a new array or creating a new list.
items
: A list of strings representing shopping items in order.fromPosition
: The current 0-based index of the item to move.toPosition
: The desired 0-based index where the item should be moved.Return the modified list of items after moving the specified item.
1 ≤ N ≤ 10⁴
(where N is the number of items in the list)0 ≤ fromPosition < N
0 ≤ toPosition ≤ N
fromPosition == toPosition
, return the list unchanged.toPosition
can be equal to N (move to the end)input: ["a", "b", "c", "d"], 1, 3 output: ["a", "c", "d", "b"] Explanation: Move "b" from index 1 to index 3.
input: ["milk", "bread", "eggs", "butter"], 2, 0 output: ["eggs", "milk", "bread", "butter"] Explanation: Move "eggs" from index 2 to index 0 (start of list).
input: ["pen", "pencil", "eraser"], 1, 1 output: ["pen", "pencil", "eraser"] Explanation: fromPosition and toPosition are the same — list remains unchanged.
input: ["apple", "banana", "carrot"], 0, 3 output: ["banana", "carrot", "apple"] Explanation: Move "apple" to the end of the list.