Implementing solution with push and pop.