I am doing a refresher on CS fundamentals.
I have a question on linked lists.
There are some cases that a dummy node is used (in the head of the list mainly) to assist in the implementation.
I see that if it is missing (i.e. if an approach without using a dummy node is followed) the code becomes “messy” (at least I do it messy since I am rusty on these stuff….)
I was wondering are there cases that an algorithm is not possible to be solved without using a dummy node?
Can you give an example?
And are there specific characteristics of the problem at hand that give away that a dummy node is the only way to go?
Note: I don’t tag a specific language but just in case my prefered is Java
No, there are not any algorithms that are not possible without having a dummy node, it is just there for convenience and keeping the code clean. Without a dummy you have to take special consideration e.g. when deleting the last node of a linked list or traversing an empty linked list – in these cases the dummy acts as a sentinel that keeps the core algorithm for traversal and removal simpler.