Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.thealgorithms.datastructures.queues;

import java.util.Queue;

/**
* Reverse a queue using recursion.
*/
public final class ReverseQueueRecursion {

private ReverseQueueRecursion() {
// private constructor to prevent instantiation
}

/**
* Reverses the given queue recursively.
*
* @param queue the queue to reverse
* @param <T> type of elements in the queue
*/
public static <T> void reverseQueue(Queue<T> queue) {
if (queue.isEmpty()) return;
T front = queue.poll();
reverseQueue(queue);
queue.add(front);
}
}
Loading