Reputation: 49665
I am implementing a linked list in Java. Is it possible to create create and use an instance variable "lastNode" in the list collection class? Or I should always access the last node through following the links of the nodes starting form the first node?
Upvotes: 1
Views: 899
Reputation: 11539
Creating a lastNode
(or tail
) reference is completely viable and in fact very useful for certain applications. Just remember, you need to update lastNode
whenever another operation such as Add()
or Remove()
might affect it.
You may also wish to look up the concept of doubly-linked lists if you're not familiar with them already. Depending on your applications, they can be very effective.
Upvotes: 5