#C12978. Recursive List Reversal
Recursive List Reversal
Recursive List Reversal
In this problem, you are required to implement a recursive algorithm to reverse a list (array) of integers. Given a list of integers, your task is to output a new list where the elements appear in the reverse order of the original list.
You must solve the problem using recursion, without using any iterative loops for the reversal process.
A recursive approach involves a base case (an empty list or a single element list) and a recursive call that processes a smaller portion of the list. The recursive formula can be summarized as follows:
[ reverse(list) = \begin{cases} [] & \text{if } list = [] \ [last] + reverse(list[:-1]) & \text{otherwise} \end{cases} ]
Test your solution with multiple inputs to ensure its correctness.
inputFormat
The input consists of two lines. The first line contains a single integer (n) ((n \geq 0)), representing the number of elements in the list. The second line contains (n) space-separated integers representing the list to be reversed.
outputFormat
Output a single line containing the reversed list as space-separated integers. If the list is empty, print an empty line.## sample
5
1 2 3 4 5
5 4 3 2 1