I'm trying to learn how to implement a LinkedList by hand and I notice that when the LinkedList needs to be traversed with a for loop that instead of the usual for loop structure used for traversing arrays....

for (int i = 0; i<length; i++)

what is used instead in the examples is stuff like this...

for (int i = 1; i<=length+1; i++)

I'm sure that although there aren't numerical values assigned to each Node, the counting should still be from 0 or no.

And does the head hold an Object itself or is the head just a starting point which itself just refers to the first Object at position 1. Maybe that's why?

Recommended Answers

All 4 Replies

I remember i used to initialize the head with the first linked list address in C/C++ language. But in java there are no addresses concept, Jvm handle pointer for you. So instead of making traditional linked list in java like c/c++ , why dont u try something new. Also LinkedList classes are available in java, then why are you re-inventing the wheel.

Yes I know but if you build something yourself it increases understanding of how something works

  1. In a linked list the nodes are not numbered, so it makes no difference whether you chose the count from 1 or 0 as long as you count the correct number of total entries in a consistent way.
  2. Each node in a linked list contains an Object, and a reference (pointer) to the next node. The "head" variable usually contains a reference to the first node, which contains an Object, and a reference to the second none (etc).

Perfect answer - especially part 2 which is exactly what I wanted to know. Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.