To remove a node from a linked list, we must ensure that anything it was pointing to is now pointed to by the node behind it in the list.
There are three possible scenarios:
- The node being removed is neither last not first, it is somewhere within the notional order
- The node being removed is first in the notional order of the list
- The node being removed is last in the notional order of the list
For example, to remove David from this list…
Chloe is directly in front of David in the list (so David is being pointed to by Chloe). David is directly in front of Edward (so David is pointing to Edward). Chloe’s Next pointer is therefore redirected to Edward to eliminate David. Note that David is not necessarily removed from the data structure (E.G. array variable) underlying the linked list.
If we now want to remove Abigail who is first in the notional order of the list…
Abigail is directly in front of Beatrix (so Abigail is pointing to Beatrix). There is nobody pointing to Abigail. Abigail’s Next pointer is therefore removed, and Beatrix is set as the new starting node.
If we now want to remove Francis from the list…
Francis is last in the list so is pointing to nobody. Francis is being pointed to by Edward. Edwards Next pointer is therefore removed to eliminate Francis.
Pseudocode for removing an item from a linked list