Queue Reversal | Practice | GeeksforGeeks

Given a Queue Q containing N elements. The task is to reverse the Queue. Your task is to complete the function rev(), that reverses the N elements of the queue.
Example 1:
Input:
6
4 3 1 10 2 6

Output: 
6 2 10 1 3 4

Explanation: 
After reversing the given
elements of the queue , the resultant
queue will be 6 2 10 1 3 4.

Example 2:
Input:
4
4 3 2 1 

Output: 
1 2 3 4

Explanation: 
After reversing the given
elements of the queue , the resultant
queue will be 1 2 3 4.
Your Task:
 You only need to complete the function rev that takes a queue as parameter and returns the reversed queue. The printing is done automatically by the driver code.
Expected Time Complexity : O(n)
Expected Auxilliary Space : O(n)
Constraints:
1 ≤ N ≤ 105
1 ≤ elements of Queue ≤ 105
0
hrithikjain98893 days ago
Total Time Taken:
0.17/1.31
queue<int> rev(queue<int> q){   // add code here.   stack<int> s;   while(q.size()!=0)   {       s.push(q.front());       q.pop();   }   while(s.size()!=0)   {       q.push(s.top());       s.pop();   }   return q;}
0
gurankitbehal781145 days ago
queue<int> rev(queue<int> q){queue<int>t;int s=q.size();vector<int>vect;int x=0;   for(int i=0;i<=s;i++){     x=q.front();     vect.push_back(x);     q.pop();   }   int i=s-1;  while(i!=-1){     t.push(vect[i]);       i--;   }   return t;}
0
saksham_bhati5 days ago
void solve(queue<int>& q)
{
    // base case
    if(q.empty())
        return;
        
    int ele = q.front();
    q.pop();
    
    solve(q);
    
    q.push(ele);
}

queue<int> rev(queue<int> q)
{
   
    solve(q);
    
    return q;
}
0
lovedeep79051 week ago
//Reverse queue using recursion
 
class GfG{   //Function to reverse the queue.static int size=0;   public Queue<Integer> rev(Queue<Integer> q){      size=q.size();       if(q.size()==0){           return q;       }              int temp=q.remove();           rev(q);           insert(q,temp);           return q;    }   static void insert(Queue<Integer> q,int temp){       if(q.isEmpty() || q.size()!=size){           q.add(temp);           return;             }        }}
 
-1
harshscode2 weeks ago
   stack<int> s;   while(!q.empty())   {       s.push(q.front());       q.pop();   }  while(!s.empty())  {      q.push(s.top());      s.pop();        }  return q;
0
sakshisuman982 weeks ago
queue<int> rev(queue<int> q){   stack<int>s;   while(!q.empty())   {       int x=q.front();       q.pop();        s.push(x);   }      while(!s.empty())   {       int y=s.top();       s.pop();       q.push(y);   }      return q;   }
 
0
f2020170712 weeks ago
struct Queue* rev(struct Queue* Q){    if(Q->size == 1) return Q;      int last = front(Q);   dequeue(Q);   struct Queue* rev_upto_last = rev(Q);   enqueue(rev_upto_last,last);      return rev_upto_last;      //code here}
0
amank07533 weeks ago
Why is this not working ??
queue<int> rev(queue<int> q){   // add code here.   if(q.size()==1)   return q;      int t=q.front();   q.pop();      rev(q);   q.push(t);      return q;}
0
harshilrpanchal19981 month ago
 public Queue<Integer> rev(Queue<Integer> q){       //add code here.       Stack<Integer> stack = new Stack<>();                     while(!q.isEmpty()){           stack.add(q.peek());           q.remove();       }       while(!stack.isEmpty()){           q.add(stack.peek());           stack.pop();       }              return q;   }
0
monish00011 month ago
queue<int> rev(queue<int> q)
{
    // add code here.
     //code here
    queue<int> ans;
    stack<int> st;
    while(!q.empty()){
        st.push(q.front());
        q.pop();
    }
    while(!st.empty()){
        ans.push(st.top());
        st.pop();
    }
    return ans;
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.