Linked lists are fundamental linear data structures where elements (nodes) are connected via pointers rather than stored contiguously in memory. Unlike arrays, linked lists provide efficient insertion and deletion operations at the cost of direct access.
Each node contains data and one or more pointers to other nodes. The three primary variants—singly, doubly, and circular—differ in their pointer structure and traversal capabilities.
Key characteristics:
The simplest form of linked list where each node contains data and a single pointer to the next node. The last node points to null.
Structure:
struct Node {
int data;
Node* next;
};
Advantages:
Disadvantages:
Common use cases:
Each node contains data and two pointers: one to the next node and one to the previous node. This enables efficient bidirectional traversal and deletion.
Structure:
struct Node {
int data;
Node* next;
Node* prev;
};
Advantages:
Disadvantages:
Common use cases:
A variation where the last node points back to the first node (or head), forming a circle. Can be implemented as singly or doubly circular.
Singly circular: Last node's next points to head.
Doubly circular: Last node's next points to head, and head's prev points to last node.
Advantages:
Disadvantages:
Common use cases:
| Operation | Singly | Doubly | Circular |
|---|---|---|---|
| Insert at head | O(1) | O(1) | O(1) |
| Insert at tail | O(n) or O(1)* | O(1)* | O(1)* |
| Delete at head | O(1) | O(1) | O(1) |
| Delete given node | O(n) | O(1) | O(1) or O(n) |
| Search | O(n) | O(n) | O(n) |
| Access by index | O(n) | O(n) | O(n) |
| Reverse traversal | O(n) space | O(1) | O(1) if doubly |
* With tail pointer maintained
Space complexity:
When to use singly linked lists:
When to use doubly linked lists:
When to use circular linked lists:
vs. Arrays:
Implementation tips: