Data Structures – Should Linked Lists Always Have a Tail Pointer?

data structureslinked list

My understanding…

Advantages:

  • Inserting at the end is O(1) instead of O(N).
  • If the list is a Doubly Linked List, then removing from the end is also O(1) instead of O(N).

Disadvantage:

  • Takes up a trivial amount of extra memory: 4-8 bytes.
  • The implementer has to keep track of the tail.

Looking at these advantages and disadvantages, I can't see why a Linked List would ever avoid using a tail pointer. Is there something I'm missing?

Best Answer

You are correct, a tail pointer never hurts and can only help. However, there is a situation where one does not need a tail pointer at all.

If one is using a linked list to implement a stack, there is no need for a tail pointer because one can guarantee that all accesses, insertions, and removals occur at the head. That being said one might use a doubly-linked list with a tail pointer anyway because that is the standard implementation in a library or platform and memory is cheap, but one does not need it.

Related Topic