You need to implement a stack using singly linked list so that POP and PUSH takes O(1) similar performance of an array. How would you do that?
We know about LIFO in a stack. We have to just maintain the element added to the linked list at the top. Then you will get a PUSH at the top with O(1) and POP at the top with O(1).
- Add elements to the head node
- Remove elements also from the head node