Linked List Concept
A linked list here is built from ListItem objects:
public class ListItem {
public int value;
public ListItem next;
public ListItem(int value) {
this.value = value;
}
}
A list is referenced by its first item, head. The last item’s next is null.
Solution
A linked list is a sequence of items where each item holds a value and a reference to the next item. An array stores its elements in one contiguous block, so you can reach any index by computing an address. A linked list’s items are separate objects. They can be anywhere in memory. The only thing connecting them is the next references, so to reach an item you follow those references from the one before it.
Solution
The head is the reference to the first item, and it is the only reference the caller keeps to the whole list. There is no indexing into a linked list, so the only way to reach any other item is to start at the head and follow next references.
Solution
The last item’s next is null. That null is the end-of-list signal.