DEV Community

tartope
tartope

Posted on • Updated on

Singly Linked List Series: Creating push() and pop() methods with JavaScript

Adding a node to the end of a Singly Linked List and removing a node from the end of a Singly Linked List is less difficult once you visually map out what needs to happen. Here is my visualization for creating a push() method that adds a node to the end of a Singly Linked List:

Image description
Image description

Edge case: you can account for an empty list. If it is not empty then follow the steps mentioned above.

Here is an example of code for a push() method:

Image description

Here is my visualization for creating a pop() method that removes a node from the end of a Singly Linked List:

Image description
Image description

Edge case: you can account for the list becoming empty. If it becomes empty then follow the steps in the example code below.
Here is an example of code for a pop() method:

Image description

Mapping out these visualizations helped increase my understanding of creating push() and pop() methods for Singly Linked Lists. Hopefully, others will find this helpful too. Feel free to share your corrections if I got anything wrong.😀

Oldest comments (0)