Challenge: Reversing First k Elements of Queue

Can you reverse first "k" elements in a given queue? A solution is placed in the "solution" section for your help, but we would suggest you solve it on your own first.

Problem Statement

Implement the function reverseK(queue, k) which takes a queue and a number “k” as input and reverses the first “k” elements of the queue. An illustration is also provided for your understanding.

Output

The queue with first “k” elements reversed. Remember to return the queue itself!

In case the value of “k” is larger than the size of the queue, is smaller than 0, or if the queue is empty, simply return None instead.

Sample Input

Queue = [1,2,3,4,5,6,7,8,9,10], k = 5

Sample Output

Queue = [5,4,3,2,1,6,7,8,9,10]

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.