Web Toolbar by Wibiya

Pages

Tuesday, April 19, 2011

In a linked list, delete a node with only a pointer given to that node.

Solution #1: Store the address of the next node in a temporary pointer. Copy the contents of the next node in the given node and delete the next node. Here is the code,

void deleteArbitNode(node* lptr)
{
    node* temp = lptr->next;


    lptr->data = lptr->next->data;
    lptr->next = lptr->next->next;

    free(temp);
}


Also note that this approach is not possible for deleting the last node.

1 comment:

Unknown said...

What if the next node is NULL?