568. Reverse a Stack Using Recursion

EasyStackStackRecursion

Given a stack represented as an array (index 0 is the bottom, the last element is the top), return the stack reversed so that the old bottom becomes the top. The input is JSON {stack}.

Input: JSON {stack}.

Output: Array — the reversed stack (bottom to top).

Examples

Example 1
Input: {"stack":[1,2,3,4]}
Output: [4,3,2,1]
Explanation: Top and bottom swap ends.
Example 2
Input: {"stack":[]}
Output: []
Explanation: Empty stays empty.
Example 3
Input: {"stack":[9]}
Output: [9]
Explanation: Single element unchanged.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →