File tree 1 file changed +42
-0
lines changed
Course 2 - Data Structures in JAVA/Lecture 10 - Queues
1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -27,3 +27,45 @@ Sample Input 2:
27
27
Sample Output 2:
28
28
8 7 6 5 2 4 3
29
29
*/
30
+ import java.util.LinkedList;
31
+ import java.util.Queue;
32
+
33
+ public class Solution {
34
+
35
+ public static Queue<Integer> reverseKElements(Queue<Integer> input, int k)
36
+ {
37
+ //Your code goes here
38
+ if (input.size()>k)
39
+ {
40
+ k=k%input.size();
41
+ }
42
+ if (k==0 || k==1)
43
+ {
44
+ return input;
45
+ }
46
+
47
+ reverseQueue(input,k);
48
+
49
+ for (int i=0;i<input.size()-k;i++)
50
+ {
51
+ input.add(input.remove());
52
+ }
53
+
54
+ return input;
55
+
56
+ }
57
+
58
+ public static void reverseQueue(Queue<Integer> input, int k)
59
+ {
60
+ //Your code goes here
61
+ if (input.size()==0 || input.size()==1 || k==0)
62
+ {
63
+ return;
64
+ }
65
+
66
+ int temp=input.remove();
67
+ reverseQueue(input,k-1);
68
+ input.add(temp);
69
+ }
70
+
71
+ }
You can’t perform that action at this time.
0 commit comments