C++ Program for Deleting a Node in a Linked List
Last Updated :
26 Aug, 2024
Write a C++ program to delete a node from the given link list.
Examples
Input: Linked List: 10 -> 20 -> 30 -> 40 -> 50, Position to delete: 3
Output: 10 -> 20 -> 40 -> 50
Explanation: The node at position 3 is removed. The list then connects node 20 directly to node 40.
Input: Linked List: 5 -> 15 -> 25, Position to delete: 1
Output: 15 -> 25
Explanation: The head node is deleted. The head pointer is updated to point to the next node.
Types of Delete Operation on a Linked List
There are three basic types of delete operations on a singly linked list:
1. Delete the Head Node
When deleting the head node, we need to adjust the head pointer to point to the next node otherwise, the whole linked list can be lost.
Following is the approach to remove the head node of the linked list:
- Store the current head node in a temporary variable.
- Move the head pointer to the next node.
- Delete the temporary node.
Implementation
C++
// C++ program to show how to delete a head node from
// the linked list
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *next;
Node(int data) {
this->data = data;
this->next = nullptr;
}
};
// Function to delete the head node
void deleteHead(Node *&head) {
if (head == nullptr) {
cout << "List is empty, nothing to delete." << endl;
return;
}
// Store current head in a temporary variable
Node *temp = head;
// Move head to the next node
head = head->next;
// Delete the old head node
delete temp;
}
int main() {
// Create a simple linked list: 3 -> 12 -> 15 -> 18
Node *head = new Node(3);
head->next = new Node(12);
head->next->next = new Node(15);
head->next->next->next = new Node(18);
cout << "Original List: ";
Node *temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
deleteHead(head); // Deleting the head node
cout << "List after deleting head: ";
temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
return 0;
}
OutputOriginal List: 3 -> 12 -> 15 -> 18 -> NULL
List after deleting head: 12 -> 15 -> 18 -> NULL
Time Complexity: O(1)
Auxiliary Space: O(1)
2. Delete the Tail Node
To delete the tail node, traverse the list to find the second-to-last node, and then set its next pointer to NULL.
Following is the approach to remove the head node of the linked list:
- Traverse the list to find the second-to-last node.
- Set the next pointer of the second-to-last node to NULL.
- Delete the last node.
Implementation
C++
// C++ program to show how to delete a head node from
// the linked list
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *next;
Node(int data) {
this->data = data;
this->next = nullptr;
}
};
void deleteTail(Node*& head) {
// Check if the list is empty
if (head == nullptr) {
cout << "List is empty, nothing to delete." << endl;
return;
}
// Check if it contains only one element
if (head->next == nullptr) {
delete head;
head = nullptr;
return;
}
// Traverse to the tail node
Node* temp = head;
while (temp->next->next != nullptr) {
temp = temp->next;
}
// delete the tail node
delete temp->next;
temp->next = nullptr;
}
int main() {
// Create a simple linked list: 3 -> 12 -> 15 -> 18
Node *head = new Node(3);
head->next = new Node(12);
head->next->next = new Node(15);
head->next->next->next = new Node(18);
cout << "Original List: ";
Node *temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
deleteTail(head); // Deleting the tail node
cout << "List after deleting head: ";
temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
return 0;
}
OutputOriginal List: 3 -> 12 -> 15 -> 18 -> NULL
List after deleting head: 3 -> 12 -> 15 -> NULL
Time Complexity: O(N), where N is the number of nodes in the list.
Auxiliary Space: O (1)
3. Delete a Node at a Specific Position
To delete a node at a specific position, traverse the list to reach the node just before the target position, adjust pointers to skip the target node, and delete it.
Following is the approach to remove the head node of the linked list:
- If the position is 1, delete the head. Otherwise,
- Traverse the list to reach the node before the target position.
- Adjust pointers to bypass the target node and point to then next of the target node.
- Delete the target node.
Implementation
C++
// C++ program to show how to delete a head node from
// the linked list
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *next;
Node(int data) {
this->data = data;
this->next = nullptr;
}
};
// Function to delete a node at a specific position
void deleteAtPosition(Node*& head, int pos) {
if (head == nullptr || pos < 1) {
cout << "Invalid position or list is empty." << endl;
return;
}
// Deleting head node
if (pos == 1) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
// Traverse to the node before the target position
Node* current = head;
for (int i = 1; i < pos - 1 && current != nullptr; i++) {
current = current->next;
}
// If position is greater than the number of nodes
if (current == nullptr || current->next == nullptr) {
cout << "Position exceeds list length." << endl;
return;
}
// Delete the node at the target position
Node* temp = current->next;
current->next = temp->next;
delete temp;
}
int main() {
// Create a simple linked list: 3 -> 12 -> 15 -> 18
Node *head = new Node(3);
head->next = new Node(12);
head->next->next = new Node(15);
head->next->next->next = new Node(18);
cout << "Original List: ";
Node *temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
deleteAtPosition(head, 2); // Deleting node at position 3
cout << "List after deleting head: ";
temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
return 0;
}
OutputOriginal List: 3 -> 12 -> 15 -> 18 -> NULL
List after deleting head: 3 -> 15 -> 18 -> NULL
Time Complexity: O(N), where N is the number of nodes in the list.
Auxiliary Space: O (1)
Conclusion
Deleting a node from a linked list is a common operation that requires careful handling of pointers to avoid breaking the list structure. Depending on the position of the node to be deleted (head, tail, or a specific position), the approach varies slightly. These examples demonstrate how to handle different deletion scenarios in a singly linked list effectively.
Similar Reads
C++ Program For Deleting A Node In A Doubly Linked List Pre-requisite: Doubly Link List Set 1| Introduction and Insertion Write a function to delete a given node in a doubly-linked list. Original Doubly Linked List Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Approach: The deletion of a node in a doubly-linked list
4 min read
C++ Program For Deleting A Given Node In Linked List Under Given Constraints Given a Singly Linked List, write a function to delete a given node. Your function must follow the following constraints: It must accept a pointer to the start node as the first parameter and node to be deleted as the second parameter i.e., a pointer to the head node is not global.It should not retu
4 min read
C++ Program For Writing A Function To Delete A Linked List Algorithm For C++:Iterate through the linked list and delete all the nodes one by one. The main point here is not to access the next of the current pointer if the current pointer is deleted. Implementation: C++ // C++ program to delete a linked list #include <bits/stdc++.h> using namespace std
2 min read
C++ Program For Deleting Last Occurrence Of An Item From Linked List Using pointers, loop through the whole list and keep track of the node prior to the node containing the last occurrence key using a special pointer. After this just store the next of next of the special pointer, into to next of special pointer to remove the required node from the linked list. C++ //
6 min read
C++ Program To Delete Alternate Nodes Of A Linked List Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3. Recomm
3 min read
C++ Program For Making Middle Node Head In A Linked List Given a singly linked list, find middle of the linked list and set middle node of the linked list at beginning of the linked list. Examples: Input: 1 2 3 4 5 Output: 3 1 2 4 5 Input: 1 2 3 4 5 6 Output: 4 1 2 3 5 6 The idea is to first find middle of a linked list using two pointers, first one moves
3 min read
C++ Program To Delete Middle Of Linked List Given a singly linked list, delete the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the linked list should be modified to 1->2->4->5 If there are even nodes, then there would be two middle nodes, we need to delete the second middle element. For example, if g
4 min read
C++ Program To Delete N Nodes After M Nodes Of A Linked List Given a linked list and two integers M and N. Traverse the linked list such that you retain M nodes then delete next N nodes, continue the same till end of the linked list.Difficulty Level: Rookie Examples: Input: M = 2, N = 2 Linked List: 1->2->3->4->5->6->7->8 Output: Linked L
3 min read
C++ Program For Writing A Function To Get Nth Node In A Linked List Write a C++ Program to GetNth() function that takes a linked list and an integer index and returns the data value stored in the node at that index position. Example: Input: 1->10->30->14, index = 2Output: 30 The node at index 2 is 30Recommended: Please solve it on "PRACTICE" first, before m
4 min read
Delete all Non-Prime Nodes from a Singly Linked List Given a singly linked list containing N nodes, the task is to delete all nodes from the list which are not prime. Examples: Input : List = 15 -> 16 -> 6 -> 7 -> 17 Output : Final List = 7 -> 17 Input : List = 15 -> 3 -> 4 -> 2 -> 9 Output : Final List = 3 ->2 Approach:
15+ min read