Practice Problem
- Queue
- Recursion
Remove the front element recursively, reverse the remaining queue, then append the removed element to the back.
O(n)
O(n)
import java.util.*;
class Solution {
public Queue<Integer> reverseQueue(Queue<Integer> queue) {
if (queue.isEmpty()) return queue;
int front = queue.poll();
reverseQueue(queue);
queue.offer(front);
return queue;
}
}