Linkedlist head node | Sololearn: Learn to code for FREE!

+1

Linkedlist head node

I still kind of get confused with head nodes. If you were to create a new node ‘Node temp’ and set it equal to the head node, would temp equal the node after head? Cause head is null by default I believe. I thought to loop over a linked list, the first iteration has to be temp.next (head.next’s value) because head is null. Can clear my confusion?

11/15/2019 4:05:27 PM

Jamie Charles

3 Answers

New Answer

+2

In a LinkedList you normally save the first Node of the List as head to have an entry point. When the list is empty you can set head of null. So before you iterate through a LinedList you should check if head is null first. I would recomment to make a Node (named p for example) to iterate through the list if head is not null. To get the value, a node normally has an attribute "value" and a connection to the next node with "next". If next is null you reached the end of the linked list.

+2

You can make a while loop for example: Node p = head; while (p != null){ .... do your stuff.... p = p.next; }

+1

Jamie Charles Please take a look at this little Tutorial https://beginnersbook.com/2013/12/linkedlist-in-java-with-example/ Regards kiuziu