Reverse a Character List/Array Recursively
You are given a list of characters. Reverse the list in place using recursion.
Requirements
- You must modify the given list in place.
- You must use recursion to reverse the characters.
- Loops such as
for, while, and do-while are not allowed.
- Built-in reversal methods such as
Collections.reverse are not allowed.
- Do not create another list, array, or string.
- Apart from the recursive call stack, use only
O(1) extra memory.
Method Signature
void reverseRecursively(List<Character> characters)
Parameters
characters: the mutable list of characters that must be reversed.
Return Value
The method does not return a value. It modifies characters directly.
Constraints
1 ≤ characters.size() ≤ 1,000
- Every element is a printable ASCII character.
characters is mutable and contains no missing values.
Examples
Example 1
Method call: reverseRecursively(characters = ['c', 'o', 'd', 'e'])
Modified list: ['e', 'd', 'o', 'c']
Example 2
Method call: reverseRecursively(characters = ['R', 'a', 'c', 'e', 'c', 'a', 'r'])
Modified list: ['r', 'a', 'c', 'e', 'c', 'a', 'R']
Example 3
Method call: reverseRecursively(characters = ['x'])
Modified list: ['x']