What is head and tail of a list?
In a linked list head and tail are special pointers to the first and last elements of the list.
1. What head is
- Head is the first element of the list.
- Traversal starts from it: each element stores a reference (pointer) to the next one.
Example:
javascript
head → [10 | *] → [20 | *] → [30 | None]Here head points to the node with value 10.
2. What tail is
- Tail is the last element of the list.
- Its "reference to the next element" (
next) equalsNone(ornull) - meaning the list has ended.
In the example above:
javascript
tail = [30 | None]3. Why they are needed
- head lets you start traversal from the beginning.
- tail lets you quickly add new elements to the end without walking through the whole list.
Without tail, adding an element to the end would require walking through the entire list from head.
4. Types of lists
| List type | Features |
|---|---|
| Singly linked | A node stores a reference only to the next element (next). |
| Doubly linked | Each node stores references to the previous and next elements (prev and next), so you can move in both directions. |
| Circular | tail points back to head, forming a ring. |
Summary:
Head is the beginning of the list, Tail is its end. Together they define the boundaries of the linked list and help perform insertions and traversal efficiently.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.