Reverse a Linked List (Array Representation)
Maintained by Vikas D, 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.
Examples
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.
Constraints
- 0 <= nums.length <= 5000
- -5000 <= nums[i] <= 5000
- Index 0 represents the head node and the last index represents the tail.
Visible test cases
- reverseList([1,2,3,4,5]) == [5,4,3,2,1]
- reverseList([]) == []
- reverseList([1]) == [1]
- reverseList([1,2]) == [2,1]
- reverseList([-1,0,1]) == [1,0,-1]
Hints
Hint 1
What data structure hands items back in the reverse order you put them in?
Hint 2
Push every value onto a stack while scanning left to right.
Hint 3
Once the stack holds all values, pop until it is empty and collect each popped value into the answer.
Approaches
Stack push then pop
Time O(n) · Space O(n)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.
- Create an empty stack.
- Walk the array from start to end and push each value onto the stack.
- Create an empty result array.
- Pop from the stack until it is empty, appending each popped value to the result.
- Return the result array.
Two-pointer in-place swap
Time O(n) · Space O(n)Reversing an array is symmetric, so swapping the ends and walking the two pointers toward the middle reverses it without an auxiliary stack.
- Copy the input into a result array so the caller's array is left untouched.
- Set a left pointer at index 0 and a right pointer at the last index.
- While left is less than right, swap the two values and move left forward and right backward.
- Return the result once the pointers meet.
Common mistakes
- Forgetting the empty-array case and indexing into a list that has no elements.
- Mutating the input array in place when the caller may still need the original order.
- Reading the array as tail-first; index 0 is the head, so the first value must end up last.
- Treating negative numbers as something special; they reverse position like any other value.
Follow-up variations
- If the list were given as real linked-list nodes with next pointers, reverse it in place by rewiring the pointers instead of building an array.
- Reverse only the segment between positions m and n and leave the rest in place.
- Reverse the list in groups of k, leaving any final group shorter than k untouched.
- Do it without a stack and without extra space beyond the result itself.