Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Head node in linked lists

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.

like image 504
user42155 Avatar asked Dec 20 '10 01:12

user42155


People also ask

What does head node mean?

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.

Is head a separate node in linked list?

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).

What is the head of a linked list used for?

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.

Is head the first node?

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.


2 Answers

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.

like image 52
ACoolie Avatar answered Nov 11 '22 16:11

ACoolie


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.

like image 35
svKris Avatar answered Nov 11 '22 18:11

svKris