Doubly Linked List Operations
A Doubly Linked List (DLL) is an advanced linear data structure where each node contains three parts:
-
Data
-
Pointer to the previous node
-
Pointer to the next node
This two-way linking allows forward and backward traversal, making certain operations more efficient than in singly linked lists.

Structure of a Doubly Linked List Node
struct Node {
int data;
Node* prev;
Node* next;
};
You may visit Lesson 06: More Operations of Singly Linked List (With C++ Examples)
1οΈβ£ Insert at the Beginning
π Definition
Inserts a new node at the start (head) of the doubly linked list.
π‘ Real-World Use Case
Adding a recently opened file to the beginning of a history list.

π» C++ Code
void insertAtStart(Node*& head, int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->prev = nullptr;
newNode->next = head;
if (head != nullptr)
head->prev = newNode;
head = newNode;
}
2οΈβ£ Insert at the End
π Definition
Adds a new node at the end (tail) of the list.
π‘ Real-World Use Case
Appending songs to the end of a music playlist.

π» C++ Code
void insertAtEnd(Node*& head, int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = nullptr;
if (head == nullptr) {
newNode->prev = nullptr;
head = newNode;
return;
}
Node* temp = head;
while (temp->next != nullptr)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
3οΈβ£ Insert After a Specific Value
π Definition
Inserts a new node after a given value.
π‘ Real-World Use Case
Inserting a new step after an existing step in a workflow.

π» C++ Code
void insertAfterValue(Node* head, int target, int value) {
Node* temp = head;
while (temp != nullptr && temp->data != target)
temp = temp->next;
if (temp != nullptr) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = temp->next;
newNode->prev = temp;
if (temp->next != nullptr)
temp->next->prev = newNode;
temp->next = newNode;
}
}
LAB 03: Doubly Linked List
4οΈβ£ Delete from the Beginning
π Definition
Deletes the first node of the doubly linked list.
π‘ Real-World Use Case
Removing the oldest entry from a cache.

π» C++ Code
void deleteFromStart(Node*& head) {
if (head == nullptr) return;
Node* temp = head;
head = head->next;
if (head != nullptr)
head->prev = nullptr;
delete temp;
}
5οΈβ£ Delete from the End
π Definition
Deletes the last node of the list.
π‘ Real-World Use Case
Removing the last undo operation.

π» C++ Code
void deleteFromEnd(Node*& head) {
if (head == nullptr) return;
if (head->next == nullptr) {
delete head;
head = nullptr;
return;
}
Node* temp = head;
while (temp->next != nullptr)
temp = temp->next;
temp->prev->next = nullptr;
delete temp;
}
π Time Complexity Summary
| Β Β Operation | Time Complexity |
|---|---|
| Insert at Start | O(1) |
| Insert at End | O(n) |
| Insert After Value | O(n) |
| Delete from Start | O(1) |
| Delete from End | O(n) |
| Traversal | O(n) |
β Advantages of Doubly Linked List
-
Bidirectional traversal
-
Easier deletion operations
-
Efficient backward navigation
β Disadvantages
-
Extra memory overhead
-
More complex pointer handling
π― Key Takeaway
A doubly linked list is ideal when two-way traversal and efficient deletion are required, such as in navigation systems, editors, and operating systems.
Visit NextΒ Lesson 08: Circular Linked List
Are you interested in developing mobile application development skills? You may visit the link below:
https://onlineskilllab.com/2025/11/18/how-to-build-a-complete-flutter-authentication-system-with-firebase-login-signup-reset-password-dashboard/
If you are learning and enjoying, subscribe to our blog for daily updates.