Thursday, December 27, 2007

Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?

1 comments:

Admin said...

copy the data from the next node to the current node and delete next node.

but fails if the current node itself is the last node ofthe list.


if(node->next)
{
//copy the data
node->value=node->next->value;

// delete the node.

temp=node->next;
node->next=temp->next;
free(temp);

}
else
{
printf("cant delete already at the last node");
}