Maintained by Vikas Dulgunde, software engineer
You are handed a singly linked list, but instead of pointer-based nodes it arrives as a plain array. The element at index 0 is the head, the next element is the node it points to, and so on until the tail at the final index.
Your job is to produce the same list traversed from tail to head. The value that was last should now come first, and the value that was the head should end up last.
Return the reversed sequence as a new array. The brief asks for a stack-based method, which fits the problem naturally: pushing every value and then popping them back out hands you the values in exactly the opposite order.
An empty input is valid and should give back an empty array. A single value comes back unchanged.
Reading the list from tail to head gives 5, then 4, 3, 2, and finally the old head 1.
There are no nodes, so the reversed list is also empty.
Negative values are ordinary values; only their position flips. The tail 1 leads and the head -1 trails.
Visible test cases
What data structure hands items back in the reverse order you put them in?
Push every value onto a stack while scanning left to right.
Once the stack holds all values, pop until it is empty and collect each popped value into the answer.
A stack is last-in first-out, so feeding values in head-to-tail order and draining it yields tail-to-head order for free.
Reversing an array is symmetric, so swapping the ends and walking the two pointers toward the middle reverses it without an auxiliary stack.