Deleting node in linklist without knowing head of linklist [duplicate]
Possible Duplicate:
Deleting a middle n开发者_开发问答ode from a single linked list when pointer to the previous node is not available
Signature of delete node is given as void delete(struct node *nodeToBeDeleted); head of the linklist is not provided. Is there any way to delete nodeToBeDeleted node?
Copy data of the next node to current node.
Now next node containing current node's data becomes the node to be deleted.
Set next of current node to next of next node.
Code Snippet
void delete(struct node *nodeToBeDeleted)
{
struct node *nextNode;
if(nodeToBeDeleted == NULL)
{
return;
}
nextNode = nodeToBeDeleted ->next;
nodeToBeDeleted ->data = nextNode->data;
nodeToBeDeleted->next = nextNode->next;
delete nextNode;
return;
}
精彩评论