# Delete node using one pointer
We are given a pointer to any Node in the linked list and we have to delete that node using the given pointer (ptr) and we dont have the access to head pointer.
Note : The Node to be deleted is not the last node in the list.
We cannot delete the node directly as it would break the links in the list as we dont have the access to its previous node through which we can join the link, So we will copy the data and link part of the next node to the node pointed by ptr and then delete the next node instead.
# Source Code - C++
void deleteNode(Node *ptr)
{
//creating temporary pointer
Node *temp;
//Pointing temp to link part of current node i.e. next node
temp=ptr->link;
//copy data and link part of next node to current node
ptr->data=temp->data;
//point current node to link part of next node
ptr->link=temp->link;
//Delete current node
free(temp);
}
Learn More
- Delete node using one pointer