I have problems with understanding what is the nature of the first node, or the so called head, in a linked list data structure. A linked list consists of nodes, and each node contains some data and a link to another node in the list. But is the very first node a node which contains data and a link to the second node? Or is it contain only a link (and no data) to a node? I thought that the very first node in a linked list has both data and a link to another node, but in one introductory book it is explained that the head is a node but a link that gets you to the first node. At the same time head is a variable of the type of the node. Why is it like this? (I am working in Java, if that matters). Thanks you.
A head node is often nothing more than a simply configured system that is configured to act as a middle point between the actual cluster and the outside network. When you are on a head node, you are not actually running or working on the cluster proper.
Conventionally the head node is the very first node of the linked list but from a "type" point of view it is no different than any other node. It does contain data as well as a pointer to the next node (the second node of your linked list, if it exists).
So In most jargon terms Head is just a local pointer which keeps reference to the first element of a linked list and is mostly initialised with NULL in order to distinguish an empty linked list.
Head of the list is not a node but a reference to the first node in the list. In other words, head can be considered a lvalue. In an empty list the value of head is equal to null.
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.
A @falmarri answered, you can implement it either way. Head node is similar to any other node in a Singly Linked list. It is just a starting node with which we can traverse the rest of the linked list. We can implement head as either a node or as a pointer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With