15. Delete middle element of a stack
My Approach
Time and Auxiliary Space Complexity
Code (C++)
class Solution {
public:
void deleteMid(stack<int>& s, int sizeOfStack) {
stack<int> newStack;
int cnt = sizeOfStack / 2; // number of elements present on the top of mid element
while (cnt--) {
newStack.push(s.top());
s.pop();
}
s.pop(); // Mid Element
while (!newStack.empty()) {
s.push(newStack.top());
newStack.pop();
}
}
};Contribution and Support
Last updated