We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 29ce2ef + 2f4231a commit 364ddc4Copy full SHA for 364ddc4
src/main/java/com/thealgorithms/datastructures/queues/ReverseQueueRecursion
@@ -0,0 +1,26 @@
1
+package com.thealgorithms.datastructures.queues;
2
+
3
+import java.util.Queue;
4
5
+/**
6
+ * Reverse a queue using recursion.
7
+ */
8
+public final class ReverseQueueRecursion {
9
10
+ private ReverseQueueRecursion() {
11
+ // private constructor to prevent instantiation
12
+ }
13
14
+ /**
15
+ * Reverses the given queue recursively.
16
+ *
17
+ * @param queue the queue to reverse
18
+ * @param <T> type of elements in the queue
19
20
+ public static <T> void reverseQueue(Queue<T> queue) {
21
+ if (queue.isEmpty()) return;
22
+ T front = queue.poll();
23
+ reverseQueue(queue);
24
+ queue.add(front);
25
26
+}
0 commit comments