Skip to content

Latest commit

 

History

History
45 lines (29 loc) · 584 Bytes

File metadata and controls

45 lines (29 loc) · 584 Bytes

Recursive Queue Reversal

Problem Link

Practice Problem


Pattern

  • Queue
  • Recursion

Approach

Remove the front element recursively, reverse the remaining queue, then append the removed element to the back.


Time Complexity

O(n)

Space Complexity

O(n)


Java Solution

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;
    }
}