- Reverse First K elements of Queue
put k elements in stack->s and poll the elements from queue->q.
take size of stack in variable size
add stack elements into queue
public Queue<Integer> modifyQueue(Queue<Integer> q, int k)
{
Stack<Integer> stack = new Stack<>();
int count = k;
while (count > 0) {
stack.push(q.poll());
count--;
}
int size = q.size();
while (!stack.empty()) {
q.add(stack.pop());
}
while(size > 0) {
q.add(q.poll());
size--;
}
return q;
}