Reverse the first K elements of a Queue ?

By | July 12, 2020

In this post, We will learn How to write a Java Program to Reverse the first K elements of a Queue?

First, let’s try to understand the problem statement in little more detail for better clarity:

You have given an integer K and a Queue of Integers then How would you reverse the order of the first K elements of the Queue, leaving the other elements in the same relative order.

For Example, if K = 6 and Queue has the following elements [10,20,30,40,50,60,70,80,90,100]

Then Output should be [60,50,40,30,20,10,70,80,90,100]

ClientTest.java

The program has tested for below input: 

Original Queue::
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
How many Elements You want to reverse from Start:
6
Queue After Reversing first 6 Elements:
[60, 50, 40, 30, 20, 10, 70, 80, 90, 100]

You May run this program to test for other input as well.

You May Also Like:

Queue Data Structure
Queue Implementation using an array in java
Queue Implementation using LinkedList in java

That’s all about Java Program to Reverse the first K elements of a Queue?
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *