DEV Community

Discussion on: Creating Linked Lists With JavaScript

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

If you define push/pop to work on the front of the list you can have an effective stack. One that both push and pop can be implemented in constant time.

Interestingly, as it might come up in an interview, you can do this even if you want to define it as the back. Internally you'll be storing the list in reverse order (or alternately, using a prev field rather than next) The index of get/delete and the order of print, need to be tweaked, but you can get the same result.

That is, whether you say push/pop works on the front or back, you can implement it in constant time.

The doubly-linked list lets you have constant time on both ends.