What is complexity of push and pop for a Stack implemented using a LinkedList?

Technology CommunityCategory: Big-O NotationWhat is complexity of push and pop for a Stack implemented using a LinkedList?
VietMX Staff asked 3 years ago

O(1). Note, you don’t have to insert at the end of the list. If you insert at the front of a (singly-linked) list, they are both O(1).

Stack contains 1,2,3:

[1]->[2]->[3]

Push 5:

[5]->[1]->[2]->[3]

Pop:

[1]->[2]->[3] // returning 5