Problems
Approach # 01 Using 2 Stacks
Step 1:Push S1 elements into S2.Step 2:Push the Given input element in the S1 stack.Step 3:Push S2 elements into S1.
class MyQueue {
stack <int> s1, s2;
public:
void push(int x) {
while(!s1.empty()) {
s2.push(s1.top());
s1.pop();
}
s1.push(x);
while(!s2.empty()){
s1.push(s2.top());
s2.pop();
}
}
int pop() {
if(empty())return -1;
int d = s1.top();
s1.pop();
return d;
}
int peek() {
if(empty()) return -1;
return s1.top();
}
bool empty() {
return s1.empty();
}
};Approach # 02 Using Single Stack
Q2: Reverse Queue using recursion
Q2: Reverse Queue using recursionQ3: Check Palindrome
Q3: Check PalindromeLast updated
Was this helpful?