【C++】关于头结点的一些理解
java - Head node in linked lists - Stack Overflow
These are called "dummy" header nodes, and they allow you to write general code that works for empty and non-empty lists.
Regularly, if you want to insert a Node
at the end of your list, you need two cases. If head
is null, indicating the list is empty, then you would set head
to the new Node
. If head
is not null, then you follow the next
pointers until you have the last Node
, and set the next
pointer to the new Node
.
However, if you use a dummy header, head
will never be null. It will be a Node
with a null next
pointer, which you can point to your new Node
, just how you would if the list did contain nodes.