Java Program to Reverse a Queue

Reverse a Queue in Java

Java Program to Reverse a Queue is a common problem that helps you understand how queues interact with other data structures like stacks and recursion. Since a queue follows the FIFO (First In, First Out) principle, reversing it means changing the order so that the last element becomes the first.

This problem is frequently asked in interviews and is useful for mastering queue manipulation in Java.

reverse a queue in java

Here you will learn different ways to reverse a queue, including algorithms, Java programs, example input/output, time and space complexity, and important tips.

What is Reversing a Queue means?

Queue stores elements in FIFO order.

Example:

1. Original Queue:

10 20 30 40

2. After reversing:

40 30 20 10

So, reversing a queue means changing its order completely.

Reverse a Queue using Stack in Java

Methods to Reverse a Queue in Java

Here we will study about 4 methods for Reverse a Queue in Java:

  1. Using Stack

  2. Using Recursion

  3. Using Java Collections (Built in)

Each method includes Concept, Algorithm, Java Code, Input/Output and Time & Space Complexity.

Learn DSA

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Methods to Reverse a Queue in Java

Method 1: Reverse Queue Using Stack

Steps:

  • Remove all elements from the queue
  • Push them into a stack
  • Pop from stack and insert back into queue

Since a stack follows LIFO, it automatically reverses the order. This is the most common and easiest method.

Algorithm:

  1. Create an empty stack.
  2. While queue is not empty:
    • Remove element and push into stack.
  3. While stack is not empty:
    • Pop element and insert into queue.

Java Code to Reverse a Queue

Run
import java.util.Queue;
import java.util.LinkedList;
import java.util.Stack;

public class ReverseQueueUsingStack {

    public static void reverseQueue(Queue queue) {

        Stack stack = new Stack<>();

        // Step 1: Move queue elements to stack
        while (!queue.isEmpty()) {
            stack.push(queue.poll());
        }

        // Step 2: Move stack elements back to queue
        while (!stack.isEmpty()) {
            queue.offer(stack.pop());
        }
    }

    public static void main(String[] args) {

        Queue queue = new LinkedList<>();

        queue.offer(10);
        queue.offer(20);
        queue.offer(30);
        queue.offer(40);

        System.out.println("Original Queue: " + queue);

        reverseQueue(queue);

        System.out.println("Reversed Queue: " + queue);
    }
}

Input:

10 20 30 40

Output:

Original Queue: [10, 20, 30, 40]
Reversed Queue: [40, 30, 20, 10]

Method to Reverse a Queue in Java

Method 2: Reverse Queue Using Recursion

Steps:

In this method, recursion is used instead of a stack. This works because recursion uses the call stack internally.

  • Remove front element
  • Recursively reverse remaining queue
  • Insert removed element at rear.

Note: May cause stack overflow for very large queues.

Algorithm:

  1. If queue is empty
    • Return
  2. Remove front element
  3. Reverse remaining queue recursively
  4. Insert removed element at rear

Java Code to Reverse a Queue

Run
import java.util.Queue;
import java.util.LinkedList;

public class ReverseQueueUsingRecursion {

    public static void reverseQueue(Queue queue) {

        if (queue.isEmpty()) {
            return;
        }

        int front = queue.poll();

        reverseQueue(queue);

        queue.offer(front);
    }

    public static void main(String[] args) {

        Queue queue = new LinkedList<>();

        queue.offer(5);
        queue.offer(15);
        queue.offer(25);
        queue.offer(35);

        System.out.println("Original Queue: " + queue);

        reverseQueue(queue);

        System.out.println("Reversed Queue: " + queue);
    }
}

Input:

5 15 25 35

Output:

Original Queue: [5, 15, 25, 35]
Reversed Queue: [35, 25, 15, 5]

Method to Reverse a Queue in Java

Method 3: Reverse Queue Using Java Collections

Java provides utility methods in Collections class that can reverse a list.

  • Since LinkedList implements both Queue and List, we can use this feature.
  • This is the shortest method.
  • Works only when using LinkedList (not with all queue types).

Algorithm:

  1. Create queue using LinkedList
  2. Use Collections.reverse(queue)

Java Code

Run
import java.util.Queue;
import java.util.LinkedList;
import java.util.Collections;

public class ReverseQueueUsingCollections {

    public static void main(String[] args) {

        LinkedList queue = new LinkedList<>();

        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        queue.offer(4);

        System.out.println("Original Queue: " + queue);

        Collections.reverse(queue);

        System.out.println("Reversed Queue: " + queue);
    }
}

Input:

1 2 3 4

Output:

Original Queue: [1, 2, 3, 4]
Reversed Queue: [4, 3, 2, 1]

Before reversing a queue, check:

  1. Queue is not null
  2. Queue is not empty
  3. Recursion depth is safe
  4. Correct data type is used

Example:

if (queue == null || queue.isEmpty())
    return;

Comparison of All Methods

Method Extra Space Time Space
Stack Yes O(n) O(n)
Recursion Yes O(n) O(n)
Collections No O(n) O(1)

Frequently Asked Questions

Answer:

It is a program that changes the order of elements in a queue so that the last element becomes first.

Answer:

Using a stack is the most common and safest method for reversing a queue.

Answer:

All standard methods take O(n) time.

Answer:

Using Collections.reverse() on LinkedList works in place, but most methods need extra space.

Answer:

Yes, for learning, but not recommended for very large queues due to stack overflow risk.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Stacks

  • Introduction to Stack in Data Structure
    Click Here
  • Operations on a Stack
    Click Here
  • Stack: Infix, Prefix and Postfix conversions
    Click Here
  • Stack Representation in –
    C | C++ | Java
  • Representation of a Stack as an Array. –
    C | C++ | Java
  • Representation of a Stack as a Linked List. –
    C | C++ | Java
  • Infix to Postfix Conversion –
    C | C++ | Java
  • Infix to prefix conversion in –
    C | C++ | Java
  • Postfix to Prefix Conversion in –
    C | C++ | Java

Queues

  • Queues in Data Structures (Introduction)
    Click Here
  • Queues Program in C and implementation
    Click Here
  • Implementation of Queues using Arrays | C Program
    Click Here
  • Types of Queues in Data Structure
    Click Here
  • Application of Queue Data Structure
    Click Here
  • Insertion in Queues Program (Enqueuing) –
    C | C++ | Java
  • Deletion (Removal) in Queues Program(Dequeuing) –
    C | C++ | Java
  • Reverse a Queue –
    C | C++ | Java
  • Queues using Linked Lists –
    C | C++ | Java
  • Implement Queue using Stack –
    C | C++ | Java
  • Implement Queue using two Stacks –
    C | C++ | Java

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction –
    C | C++ | Java
  • Priority Queue Implementation using Array –
    C | C++ | Java
  • Priority Queue using Linked List –
    C | C++ | Java
  • Priority Queue Insertion and Deletion-
    C | C++ | Java