Home »
Data Structure
Single Linked List Deletion
Last Updated : November 24, 2025
Deletion in a singly linked list means removing a node from the beginning, the end, or from any position inside the list. Every deletion requires correct pointer adjustments so the remaining nodes stay linked properly.
All Possible Cases of Deletion
- Deleting the first node
- Deleting the last node
- Deleting an intermediate node
Deletion Algorithms
1. Deleting the First Node
In this case, the head node is removed. The next node becomes the new head of the list. Follow these steps:
- Create a temporary pointer (e.g., temp) to store the current head node.
- Move the head pointer to the next node in the list.
- Delete the temp node.
2. Deleting the Last Node
To remove the last node, you must reach the end of the list and keep track of the node just before the last one. Steps:
- Traverse the list using two pointers: prev (previous node) and tail (current node).
- tail will point to the last node, and prev will point to the node before it.
- Set the next pointer of prev to NULL and delete the tail node.
3. Deleting an Intermediate Node
This case involves deleting a node somewhere in the middle of the list. The steps are:
- Maintain two pointers: curr (points to the node to delete) and prev (points to the previous node).
- Update the next pointer of prev to the next node of curr.
- Delete the curr node.
C implementation of deletion in a linked list
#include <stdio.h>
#include <stdlib.h>
struct node{
int data; // data field
struct node *next;
};
void traverse(struct node* head){
struct node* current=head; // current node set to head
int count=0; // to count total no of nodes
printf("\n traversing the list\n");
while(current!=NULL){ //traverse until current node isn't NULL
count++; //increase node count
printf("%d ",current->data);
current=current->next; // go to next node
}
printf("\ntotal no of nodes : %d\n",count);
}
struct node* creatnode(int d){
struct node* temp=malloc(sizeof(struct node));
temp->data=d;
temp->next=NULL;
return temp;
}
int main(){
printf("creating the linked list by inserting new nodes at the begining\n");
printf("enter 0 to stop building the list, else enter any integer\n");
int k,count=1,x;
struct node* curr,*temp,*prev;
scanf("%d",&k);
struct node* head=creatnode(k); //buliding list, first node
scanf("%d",&k);
///////////////////inserting at begining//////////////////////
while(k){
curr=creatnode(k);
curr->next=head; //inserting each new node at the begining
head=curr;
scanf("%d",&k);
}
traverse(head); // displaying the list
////////////////deleting the first node////////////////////
printf("\ndeleting the first node.............\n");
temp=head; // first node assigned to temp
head=head->next; // head node is updated
free(temp); // deleting the first node
traverse(head); // displaying the list
printf("\nfirst node deleted...............\n");
/////////////////deleting the last node////////////////
printf("\ndeleting the last node.............\n");
temp=head->next;
prev=head;
while(temp->next!=NULL){
temp=temp->next;
prev=prev->next;
}
// after traversal temp points to the last
//node and prev to the previous node of the last node
prev->next=NULL;
free(temp);
traverse(head);
printf("\last node deleted................\n");
///////////////deleting any intermediate node in the list////////////////
printf("\n enter the position of the node you want to delete\n");
scanf("%d",&x);
temp=head->next;
prev=head;
while(count!=x-1){
temp=temp->next;
prev=prev->next;
count++;
} // temp pointing to the node to be deleted and prev to the previous node
prev->next=temp->next;
free(temp);
traverse(head);
return 0;
}
Output
creating the linked list by inserting new nodes at the begining
enter 0 to stop building the list, else enter any integer
1
2
3
4
5
6
7
8
9
0
traversing the list
9 8 7 6 5 4 3 2 1
total no of nodes : 9
deleting the first node.............
traversing the list
8 7 6 5 4 3 2 1
total no of nodes : 8
first node deleted...............
deleting the last node.............
traversing the list
8 7 6 5 4 3 2
total no of nodes : 7
last node deleted................
enter the position of the node you want to delete
5
traversing the list
8 7 6 5 3 2
total no of nodes : 6
Time and Space Complexity of Linked List Deletion
Deleting the first node: O(1)
Deleting the last node: O(n) — requires traversal
Deleting an intermediate node: O(n) — traversal depends on position
Space Complexity: O(1)
Common Mistakes in Linked List Deletion
- Forgetting to update the head pointer when deleting the first node.
- Not tracking the previous node while deleting the last or intermediate node.
- Deleting a node without updating the pointer of the previous node.
- Not checking for an empty list before performing deletion.
When to Use Linked List Deletion
- When frequent deletions are required from the beginning of a list.
- When dynamic memory allocation is needed.
- When avoiding element shifting (as in arrays) is important.
FAQs
1. Is deletion faster in a linked list or an array?
Deletion is faster in a linked list because no shifting of elements is required.
2. Can we delete a node without traversal?
Only the first node can be deleted without traversal. Other positions require traversal.
3. What happens if we delete a node without updating pointers?
The remaining nodes become disconnected, leading to memory leaks or loss of data.
Advertisement
Advertisement