Skip to content

Commit 9af8567

Browse files
Update ReverseQueueRecursion.java
1 parent 51ef073 commit 9af8567

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/main/java/com/thealgorithms/datastructures/queues/ReverseQueueRecursion.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
package com.thealgorithms.datastructures.queues;
2+
23
import java.util.Queue;
4+
5+
/**
6+
* Reverse a queue using recursion.
7+
*/
38
public final class ReverseQueueRecursion {
49
private ReverseQueueRecursion() {
10+
// private constructor to prevent instantiation
511
}
12+
13+
/**
14+
* Reverses the given queue recursively.
15+
*
16+
* @param queue the queue to reverse
17+
* @param <T> the type of elements in the queue
18+
*/
619
public static <T> void reverseQueue(final Queue<T> queue) {
720
if (queue == null || queue.isEmpty()) {
821
return;
922
}
23+
1024
final T front = queue.poll();
1125
reverseQueue(queue);
1226
queue.add(front);

0 commit comments

Comments
 (0)