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:
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:
Here is my visualization for creating a pop() method that removes a node from the end of a Singly Linked List:
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:
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.😀
Top comments (0)