开发者

Deleting node in linklist without knowing head of linklist [duplicate]

开发者 https://www.devze.com 2023-03-26 02:41 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Deleting a middle n开发者_开发问答ode from a single linked list when pointer to the previous node is not availa
This question already has answers here: Closed 11 years ago.

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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消