A singly linked list allows dynamic manipulation of data through pointers. Beyond basic insertion and display, real-world applications require advanced operations such as inserting or deleting nodes at specific positions.
This section explains five important operations with logic, use cases, and C++ code.
Click to visit the previous lesson: Introduction to Linked List
1️⃣ Insert Before a Specific Value
📌 Definition
This operation inserts a new node before a given value in the linked list.
💡 Real Use Case
In a task scheduler, you may want to add a high-priority task before a specific existing task.
🔧 Algorithm
-
If the list is empty → stop
-
If the target value is at the head → insert at start
-
Traverse the list to find the node just before the target value
-
Adjust pointers accordingly

💻 C++ Code
void insertBeforeValue(Node*& head, int target, int value) {
if (head == nullptr) return;
if (head->data == target) {
insertAtStart(head, value);
return;
}
Node* temp = head;
while (temp->next != nullptr && temp->next->data != target) {
temp = temp->next;
}
if (temp->next != nullptr) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = temp->next;
temp->next = newNode;
}
}
2️⃣ Delete from Start
📌 Definition
Removes the first node (head) of the linked list.
💡 Real Use Case
Removing the oldest request from a service queue.
🔧 Algorithm
-
Check if list is empty
-
Store head in a temporary pointer
-
Move head to next node
-
Delete old head

💻 C++ Code
void deleteFromStart(Node*& head) {
if (head == nullptr) return;
Node* temp = head;
head = head->next;
delete temp;
}
3️⃣ Delete from End
📌 Definition
Deletes the last node of the linked list.
💡 Real Use Case
Undo the last operation in an application.
🔧 Algorithm
-
If list is empty → stop
-
If only one node → delete head
-
Traverse to second-last node
-
Delete last node

💻 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->next != nullptr) {
temp = temp->next;
}
delete temp->next;
temp->next = nullptr;
}
4️⃣ Delete After a Specific Value
📌 Definition
Deletes the node immediately after a given value.
💡 Real Use Case
Removing a dependent process after a completed task.
🔧 Algorithm
-
Traverse to the target value
-
If next node exists → delete it

💻 C++ Code
void deleteAfterValue(Node* head, int target) {
Node* temp = head;
while (temp != nullptr && temp->data != target) {
temp = temp->next;
}
if (temp != nullptr && temp->next != nullptr) {
Node* delNode = temp->next;
temp->next = delNode->next;
delete delNode;
}
}
5️⃣ Delete Before a Specific Value
📌 Definition
Deletes the node just before a given value.
💡 Real Use Case
Removing an invalid configuration step before a critical step.
🔧 Algorithm
-
If list has fewer than 2 nodes → stop
-
Traverse while maintaining previous pointers
-
Adjust links and delete the node

💻 C++ Code
void deleteBeforeValue(Node*& head, int target) {
if (head == nullptr || head->next == nullptr) return;
if (head->next->data == target) {
deleteFromStart(head);
return;
}
Node* prev = nullptr;
Node* curr = head;
while (curr->next != nullptr && curr->next->data != target) {
prev = curr;
curr = curr->next;
}
if (curr->next != nullptr && prev != nullptr) {
prev->next = curr->next;
delete curr;
}
}
📌 Summary of Operations
| Operation | Time Complexity |
|---|---|
| Insert Before Value | O(n) |
| Delete from Start | O(1) |
| Delete from End | O(n) |
| Delete After Value | O(n) |
| Delete Before Value | O(n) |
Singly Linked List – Interview Questions & Answers
1. What is a singly linked list?
Answer:
A singly linked list is a linear dynamic data structure where each node contains:
-
Data
-
A pointer to the next node
The last node points to NULL, indicating the end of the list.
2. How is a linked list different from an array?
Answer:
Arrays use contiguous memory and provide O(1) access, while linked lists use non-contiguous memory and allow efficient insertion and deletion without shifting elements.
3. Why is a linked list called a dynamic data structure?
Answer:
Because memory is allocated at runtime, allowing the list to grow or shrink dynamically as elements are added or removed.
4. What is the structure of a node in a singly linked list?
Answer:
struct Node {
int data;
Node* next;
};
5. What happens if the head pointer is NULL?
Answer:
It indicates that the linked list is empty.
6. What is the time complexity of inserting a node at the beginning?
Answer:
O(1) — only the head pointer is updated.
7. What is the time complexity of deleting the last node?
Answer:
O(n) — traversal is required to reach the second-last node.
8. How do you delete a node after a given value?
Answer:
-
Traverse until the target value is found
-
Store the next node
-
Adjust pointers and delete the node
9. What are the edge cases when deleting nodes in a linked list?
Answer:
-
Empty list
-
Single-node list
-
Target value not found
-
Deleting the head or last node
10. Why is pointer manipulation critical in linked lists?
Answer:
Incorrect pointer handling can cause:
-
Memory leaks
-
Loss of nodes
-
Program crashes (dangling pointers)
11. How do you delete a node before a specific value?
Answer:
You maintain two pointers:
-
One for the previous node
-
One for the current node
When the node before the target is identified, adjust pointers and delete it safely.
12. What is the disadvantage of singly linked lists?
Answer:
-
No backward traversal
-
Extra memory overhead due to pointers
-
Slower access compared to arrays
13. Can you reverse a singly linked list?
Answer:
Yes, by iteratively changing the direction of pointers using:
-
prev -
current -
next
14. What is the difference between singly and doubly linked lists?
Answer:
| Feature | Singly | Doubly |
|---|---|---|
| Pointers | One | Two |
| Memory | Less | More |
| Traversal | One direction | Both directions |
15. How do linked lists help in real-world applications?
Answer:
Linked lists are used in:
-
Music playlists
-
Browser history
-
Memory management
-
Operating systems
-
Undo/Redo operations
16. Which data structure would you use for frequent insertions and deletions?
Answer:
A linked list, because it avoids shifting elements like arrays.
17. Why are linked lists not cache-friendly?
Answer:
Because nodes are stored in non-contiguous memory locations, reducing cache locality.
18. What happens if you forget to free memory in a linked list?
Answer:
It causes a memory leak, leading to inefficient memory usage and potential application crashes.
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.