![]() |
VOOZH | about |
A linked list in C++ is a linear data structure used to store data in non-contiguous memory locations. Unlike arrays, linked lists store elements dynamically and connect them using pointers.
A linked list is represented by a pointer called head, which points to the first node of the list. Each node typically contains:
Node Structure:
Based on their structure, linked lists are classified into the following types:
A Singly linked List is the simplest form of linked list. Each node contains data and a pointer to the next node. The last node's next pointer points to NULL, indicating the end of the list.
Singly linked list can be represented as a pointer to the first node, where each node contains:
Operation | Operation Type | Description | Time Complexity | Space Complexity |
|---|---|---|---|---|
Insertion | At Beginning | Insert a new node at the start of a linked list. | O (1) | O (1) |
| At the End | Insert a new node at the end of the linked list. | O (N) | O (1) | |
| At Specific Position | Insert a new node at a specific position in a linked list. | O (N) | O (1) | |
Deletion | From Beginning | Delete a node from the start of a linked list | O (1) | O (1) |
| From the End | Delete a node at the end of a linked list. | O (N) | O (1) | |
| A Specific Node | Delete a node from a specific position of a linked list. | O (N) | O (1) | |
Traversal | Traverse the linked list from start to end. | O (N) | O (1) | |
A doubly linked list is an extension of a singly linked list where each node contains pointers to both the next and previous nodes. This allows traversal in both forward and backward directions.
Doubly linked list can be represented as pointer to the first node where each node contains:
Operation | Operation Type | Description | Time Complexity | Space Complexity |
|---|---|---|---|---|
Insertion | At Beginning | Insert a new node at the start of a linked list. | O (1) | O (1) |
| At the End | Insert a new node at the end of the linked list. | O (N) | O (1) | |
| At Specific Position | Insert a new node at a specific position in a linked list. | O (N) | O (1) | |
Deletion | From Beginning | Delete a node from the start of a linked list | O (1) | O (1) |
| From the End | Delete a node at the end of a linked list. | O (N) | O (1) | |
| A Specific Node | Delete a node from a specific position of a linked list. | O (N) | O (1) | |
Traversal | Traverse the linked list from start to end or vice versa. | O (N) | O (1) | |
A circular linked list is similar to a singly linked list, but the last node points back to the first node instead of pointing to NULL. This creates a circular structure.
Circular linked list can be represented as pointer to the first node where each node contains:
Operation | Operation Type | Description | Time Complexity | Space Complexity |
|---|---|---|---|---|
Insertion | At Beginning | Insert a new node at the start of a linked list. | O (N) | O (1) |
| At the End | Insert a new node at the end of the linked list. | O (N) | O (1) | |
| At Specific Position | Insert a new node at a specific position in a linked list. | O (N) | O (1) | |
Deletion | From Beginning | Delete a node from the start of a linked list | O (N) | O (1) |
| From the End | Delete a node at the end of a linked list. | O (N) | O (1) | |
| A Specific Node | Delete a node from a specific position of a linked list. | O (N) | O (1) | |
Traversal | Traverse the linked list from start to end or vice versa. | O (N) | O (1) | |
Following are some common applications of the linked list data structure:
Advantages of linked list are mentioned below:
Despite its flexibility and dynamic nature, a linked list has some limitations: