Home »
Data Structure
Deleting a Node from a Linked List Without Head Pointer
Last Updated : December 02, 2025
In a standard linked list, to delete a node, we usually need the head pointer of the list. However, in some scenarios, you may only have the address of the node that needs to be deleted. In such cases, the typical deletion method cannot be applied. This article explains how to delete a node when the head pointer is not given, with an example, algorithm, and step-by-step solution.
Problem Statement
Write a program to delete a node in a linked list where the head pointer is not given. Only the address of the node to be deleted is provided.
Example
Basic list:
4->5->1->6->7->NULL
Node to be deleted: 1
After deletion:
4->5->6->7->NULL
Solution
In this problem, we don’t have the head pointer of the list; we only have the address of the node to be deleted (e.g., pointer to node 1).
Effectively, we have access to the sublist starting from the node to be deleted:
1->6->7->NULL
We can delete the node by copying the value of the next node into the current node repeatedly until the last node, and then removing the last node:
1->6->7->NULL
6->6->7->NULL (copy value of next node)
6->7->7->NULL
6->7->NULL (last node removed)
Thus, the final linked list becomes:
4->5->6->7->NULL
Algorithm
1. Declare a temporary variable 'temp' to store node values.
2. Declare 'ListNode* node' and initialize it to the address of the node to be deleted.
3. While node->next is not NULL:
// Copy the value from the next node into the current node
node->data = node->next->data;
// Move to the next node
node = node->next;
4. Remove the last node:
// Find the second last node
ListNode* pre = address of the original node to delete;
ListNode* fast = pre->next;
While (fast->next != NULL):
pre = pre->next;
fast = fast->next;
End While
// Disconnect the last node
pre->next = NULL;
Notes
- This method works only if the node to be deleted is not the last node.
- Time complexity: O(n), where n is the number of nodes from the given node to the end of the list.
- Space complexity: O(1), as no extra memory is used.
C++ Implementation for Deleting a Node from a Linked List Without Head Pointer
#include <bits/stdc++.h>
using namespace std;
class ListNode{
public:
int val;
ListNode* next;
};
ListNode* creatnode(int d){
ListNode* temp=(ListNode*)malloc(sizeof(ListNode));
temp->val=d;
temp->next=NULL;
return temp;
}
void traverse(ListNode* head){
ListNode* current=head; // current node set to head
int count=0; // to count total no of nodes
printf("displaying the list\n");
//traverse until current node isn't NULL
while(current!=NULL){
count++; //increase node count
if(current->next)
printf("%d->",current->val);
else
printf("NULL\n");
current=current->next; // go to next node
}
printf("total no of nodes : %d\n",count);
}
void deleteNode(ListNode* node) {
//deleting node without head pointer
int temp;
ListNode* pre=node;
while(node->next){
pre = node;
node->val = (node->next)->val;
node=node->next;
}
pre->next=NULL;
free(node);
}
int main(){
ListNode* head=creatnode(4);
head->next=creatnode(5);
head->next->next=creatnode(1);
head->next->next->next=creatnode(6);
head->next->next->next->next=creatnode(7);
head->next->next->next->next->next=creatnode(8);
ListNode* todelete=head->next->next; //1
cout<<"deleting node with value 1\n";
cout<<"before deletion:\n";
traverse(head);
deleteNode(todelete);
cout<<"after deletion:\n";
traverse(head);
return 0;
}
Output
deleting node with value 1
before deletion:
displaying the list
4->5->1->6->7->NULL
total no of nodes : 6
after deletion:
displaying the list
4->5->6->7->NULL
total no of nodes : 5
Advertisement
Advertisement