How does a node get added to the end of a linked list?


#1

Question

This exercise discusses how to add a node to the start of a linked list. How is a node added to the end?

Answer

Each node in a linked list contains a pointer to the next node (a bi-directional or doubly linked list contains an additional pointer to the previous node). For the last node in the linked list, the next node pointer is assigned a value to indicate the end of the list such as “null” or None. When adding an item to the linked list, a new node is created. That new node has its next node pointer set to “null” or None and the existing last node of the linked list is updated to have its next node pointer reference the newly created node.