How is the end of a linked list identified?


#1

Question

How do you determine that you have reached the end of the linked list?

Answer

Each node in the linked list contains a pointer (or equivalent based on the language you are using) to the next element in the linked list. The last node in the linked list has nothing to point to for the next node, so the next node pointer is assigned a value to indicate that it is not pointing to anything valid. Depending on the language you are using, this can be a “null” value or something equivalent. In Python, the value would be None.

This is also true for the first element of a bi-directional linked list. A bi-directional linked list has pointers to the next node and the previous node. Since the first node in the list has no previous element, that pointer should also be set to a “null” value.