DSA
Stack and Queue/Implementation

Implement Stack using Queue

class Stack {
  constructor() {
    this.st = []; // SC: O(n)
  }
 
  push(val) {
    this.st.push(val);
 
    for (let i = 0; i < this.st.length - 1; i++) {
      this.st.push(this.st.shift()); // shift is O(n) so push takes O(n^2)
    }
 
    // this.st.unshift(val); // TC: O(n) if push front allow
  }
 
  pop() {
    if (this.st.length === 0) return null;
 
    return this.st.shift(); // TC: O(n)
  }
 
  peek() {
    if (this.st.length === 0) return null;
 
    return this.st[0]; // TC: O(1)
  }
}