Reputation: 85
For a singly linked list, should I store the head and size variables, or the head, tail, and size?
Upvotes: -2
Views: 40
Reputation: 665070
Only the head
field is a canonical. You can trivially implement getSize()
and getTailNode()
methods with only the head node available.
Storing size
and tail
as fields is optional, they are not a fundamental part of the "linked list" concept. Having them does benefit the complexity of certain operations though, at the cost of having to store and correctly update them.
Upvotes: 0
Reputation: 11
In a singly linked list, you can store just the head and size variables, but including the tail as well can offer several benefits depending on your use case.
Upvotes: 0