Delete a node in a linked list without a head node

Source: Internet
Author: User

Topic Description: Given a linked list, no head node, how to delete one of the random node.

Analysis: If a linked list does not have a head node, then we cannot find the previous node of the deleted node. How to follow this idea, will not be able to implement the function.
Let's change the idea of what is the purpose of deleting a node. is to reach the deleted node data is deleted, that is, we actually just need to delete the node's data, not the entire node. How to achieve it. We can remove the data from the node in the following node, then assign the value to the front and delete the node. For example: ABCD three nodes, if we need to delete the B node, then we can assign C->data to B->data, let D and b connected, delete C, can.

The code is as follows:

void Deletenode (node* pcurrent)
{
    if (pcurrent = = null)
        return null; 
    node* Pnext = pcurrent->next; 
    if (pnext! = NULL)
    {
        Pcurrent->next = pnext->next; 
        Pcurrent->data = pnext->data; 
        Delete pnext; 
    }
}

Reference:
The beauty of programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.