Generally, people ask the question to delete a node in linked list without head pointer. It is a tricky question because the general approach to delete the given node is that traverse the linked list till the node which you want to delete. In this article, I will describe that how we can delete a node without using the head pointer.
You can also see, Delete a Linked List node using the head pointer
There is no practical solution to delete a node directly by given pointer, we need to do some trick. We need to copy the data from the next node to the current node by given pointer to be deleted and delete the next node.
//Get the Address of the next node NodePointer temp = Node->pNextNode; //Get the data of next node Node->iData = temp->iData; //Get the Address of next to next node Node->pNextNode = temp->pNextNode; //Delete the next node free(temp);
Note: You can not use this technique to delete the last node.
Program to delete a node in linked list without head pointer
#include<stdio.h> #include<stdlib.h> // Creating Node struct Node { /*Data field*/ int iData; /*Node Pointer*/ struct Node *pNextNode; }; // Define the new type Node type and Node pointer typedef struct Node NodeType, * NodePointer; /* Paas the reference of the head pointer of a list and an integer data. This function use to add the node at the beginning*/ int InsertNodeAtBeginning(NodePointer * pHead, int iUserData) { int iRetValue = -1; // Call malloc to allocate memory in heap for the new node NodePointer pNewNode = malloc(sizeof(NodeType)); if( pNewNode != NULL) //Check allocated memory { pNewNode->iData = iUserData; //put the desire Data pNewNode->pNextNode = *pHead; //Give the Address of first Node *pHead = pNewNode; // Assign the Address of New Node to Head iRetValue = 0; // Update the return value } return iRetValue; } //Delete Any node of the linked list void DeleteNode(NodePointer Node) { NodePointer temp = Node->pNextNode; Node->iData = temp->iData; Node->pNextNode = temp->pNextNode; free(temp); } /* Paas the reference of the head pointer of a list and an integer data. This function use to add the node at the End*/ int InsertNodeAtEnd(NodePointer * pHead, int iUserData) { int iRetValue = -1; NodePointer pLastNode = NULL; NodePointer pNewNode = NULL; //Give the Address of first Node pLastNode = *pHead; // Call malloc to allocate memory in heap for the new node pNewNode = malloc(sizeof(NodeType)); if( pNewNode != NULL) //Check allocated memory { pNewNode->iData = iUserData; //put the desire Data pNewNode->pNextNode = NULL; //Give the Address of first Node iRetValue = 0; // Update the return value } // If there is no node in beginning if(pLastNode == NULL) { *pHead = pNewNode; } else { // Find the address of last node while( pLastNode ->pNextNode != NULL) { pLastNode = pLastNode ->pNextNode; } // Assign last node address pLastNode ->pNextNode = pNewNode; } return iRetValue; } // This function use to prints the data of the list from the begning //to the given list. void PrintTheList(NodePointer pNode) { printf("Linked List is = "); while (pNode != NULL) { printf("%d ",pNode->iData); pNode = pNode->pNextNode; } } //Create a linked list of certain number of nodes int CreateLinkedList(NodePointer *pHead, int iNumberofNode) { int iData = 0; int iRetValue = -1; int iCount = 0; for(iCount =0; iCount < iNumberofNode; iCount++) { /*Enter desire data*/ printf("\nEnter the Data = "); scanf("%d",&iData); if((*pHead) == NULL) { //Create First Node iRetValue = InsertNodeAtBeginning(pHead,iData); } else { //Add the Node at the End iRetValue = InsertNodeAtEnd(pHead,iData); } } return iRetValue; } /* Driver program to test above functions*/ int main(void) { int iRetValue = -1; int iNumberNode =0; /*Start with the empty list */ NodePointer head = NULL; printf("\nEnter the number of nodes = "); scanf("%d",&iNumberNode); //Create the linked list iRetValue = CreateLinkedList(&head,iNumberNode); if(iRetValue == -1) { printf("List is empty !\n"); return 0; } printf("\nLinked list before deleting the node\n"); //Print the list PrintTheList(head); //delete the head DeleteNode(head); printf("\nLinked list after deleting the node\n"); //Print the list PrintTheList(head); return 0; }