C++ Program for Deleting a Node in a Linked List
Last Updated :
25 Apr, 2023
We have discussed Linked List Introduction and Linked List Insertion in previous posts on a singly linked list.
Let us formulate the problem statement to understand the deletion process. Given a ‘key’, delete the first occurrence of this key in the linked list.
Iterative Method:
To delete a node from the linked list, we need to do the following steps.
1) Find the previous node of the node to be deleted.
2) Change the next of the previous node.
3) Free memory for the node to be deleted.
C++
#include <iostream>
using namespace std;
struct node
{
int data;
node *next;
};
class linked_list
{
private :
node *head,*tail;
public :
linked_list()
{
head = NULL;
tail = NULL;
}
void add_node( int n)
{
node *tmp = new node;
tmp->data = n;
tmp->next = NULL;
if (head == NULL)
{
head = tmp;
tail = tmp;
}
else
{
tail->next = tmp;
tail = tail->next;
}
}
node* gethead()
{
return head;
}
static void display(node *head)
{
if (head == NULL)
{
cout << "NULL" << endl;
}
else
{
cout << head->data << endl;
display(head->next);
}
}
static void concatenate(node *a,node *b)
{
if ( a != NULL && b!= NULL )
{
if (a->next == NULL)
a->next = b;
else
concatenate(a->next,b);
}
else
{
cout << "Either a or b is NULL\n" ;
}
}
void front( int n)
{
node *tmp = new node;
tmp -> data = n;
tmp -> next = head;
head = tmp;
}
void after(node *a, int value)
{
node* p = new node;
p->data = value;
p->next = a->next;
a->next = p;
}
void del (node *before_del)
{
node* temp;
temp = before_del->next;
before_del->next = temp->next;
delete temp;
}
};
int main()
{
linked_list a;
a.add_node(1);
a.add_node(2);
a.front(3);
a.add_node(5);
a.add_node(15);
a.after(a.gethead()->next->next->next, 10);
a.del(a.gethead()->next);
linked_list::display(a.gethead());
return 0;
}
|
Time Complexity: O(n), where n represents the length of the given linked list.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Recursive Method:
To delete a node of a linked list recursively we need to do the following steps.
1.We pass node* (node pointer) as a reference to the function (as in node* &head)
2.Now since the current node pointer is derived from the previous node’s next (which is passed by reference) so now if the value of the current node pointer is changed, the previous next node’s value also gets changed which is the required operation while deleting a node (i.e points previous node’s next to current node’s (containing key) next).
3.Find the node containing the given value.
4.Store this node to deallocate it later using free() function.
5.Change this node pointer so that it points to its next and by performing this previous node’s next also get properly linked.
Below is the implementation of the above approach.
C++
#include <iostream>
struct node{
int data;
struct node* next;
node( int val){
data = val;
next = NULL;
}
};
void delete_a_linked_list( struct node*& head){
if (head == NULL){
return ;
}
delete_a_linked_list(head->next);
std::cout<< "Deleting node " <<head->data<< "\n" ;
free (head);
head = NULL;
}
int main() {
struct node* head = new node(2);
head->next = new node(4);
head->next->next = new node(6);
head->next->next->next = new node(9);
delete_a_linked_list(head);
if (head == NULL){
std::cout<< "The Linked List is empty\n" ;
} else {
std::cout<< "The Linked List is not empty\n" ;
}
return 0;
}
|
Output
Deleting node 9
Deleting node 6
Deleting node 4
Deleting node 2
The Linked List is empty
Time Complexity: O(n), where n represents the length of the given linked list.
Auxiliary Space: O(n), due to recursive call stack where n represents the length of the given linked list.
Please refer complete article on Linked List | Set 3 (Deleting a node) for more details!
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...