![linked list stack get mentain minimum number linked list stack get mentain minimum number](https://images.contentstack.io/v3/assets/bltdd99f24e8a94d536/blte9efeb619fdde61f/6014935882548c0f8284dc10/DealoftheDay_VDAY_Dropdown_s40p.jpg)
The condition in the while loop says that we keep on pushing the popped elements of the queue till q.peek() = x (element at the front is greater than/equal to the cy=urrent element). To gain more clarity on this aspect, consider that our current element is the greatest element of the queue and we need to place it at it's correct position.
#Linked list stack get mentain minimum number code
This variable plays a very important role in ensuring that the code does not get into an infinite loop in case all elements are smaller than the current element(1st while loop) and when all elements are greater or equal to the current eleemnet(2nd while loop). If you take a closer look at the code implementation given below, you will notice that a variable count is used while popping elements from the queue. Next, we pop the remaining elements (equal or greater tahn the current element) and insert them at the end of the queue. Once we find an element equal to or greater than the current element, we insert the current element at the end of the queue. To do this, we first pop all the elements which are smaller than the current element and insert then at the end of the queue. Once this sorted queue is received, we insert our current element we need to place the current elemrnt at it's correct position. We use recursion in such a way that after the recursive call returns we have a sorted queue in the ascending order (smallest element at the front of the queue). Then, we make a recursive call for the remaining queue. We first remove the element at the front of the queue. We can also use recursion to sort a queue. So, space complexity is constant or O(1). This algorithm has time complexity of O(n^2) Space Complexityĭoes not use any extra space.
![linked list stack get mentain minimum number linked list stack get mentain minimum number](https://venturebeat.com/wp-content/uploads/2020/03/mya.jpg)
Step 4:remove elts from index 0+1 to 4-1.Step 2: remove elt from Index 0 to -1.
![linked list stack get mentain minimum number linked list stack get mentain minimum number](https://res.cloudinary.com/practicaldev/image/fetch/s--uBjUlY1q--/c_imagga_scale,f_auto,fl_progressive,h_420,q_auto,w_1000/https://res.cloudinary.com/practicaldev/image/fetch/s--wbUhsR7B--/c_imagga_scale%2Cf_auto%2Cfl_progressive%2Ch_420%2Cq_auto%2Cw_1000/https://dev-to-uploads.s3.amazonaws.com/i/jp56ny1p3mscxkem7ww1.jpg)