A linked list must end with a null
value. Without such a marker at the end of the list, a routine cannot “see” the end of the data structure. This assignment of a null
value at the end of the list is often taken care of when a new node is packaged or through the use of a constructor.
-
When a reference variable is null
, it is a programming error to invoke one of its methods or to try to access one of its instance variables. For example, a program may maintain a reference to the first node of a linked list, as follows:
ListNode first;
Initially, the list is empty and first
is the null reference. At this point, it is a programming error to invoke one of the first
’s methods. The error would occur as a NullPointerException
.