C language to achieve a single linked list of reverse order printing (lead node)

Source: Internet
Author: User

I was in the previous blog, "C language to achieve a single list (not the lead node) in reverse print" in detail to achieve a not the first node of the list of the reverse order printing, the whole idea is also very simple, that is, sequentially traversing the original linked list, and then the removed node with the head interpolation method to establish a new linked list, the new linked list is the original list of reverse order. This blog I will be implemented using the lead node of the linked list to achieve reverse order, the idea is the same as above. Code uploaded to Https://github.com/chenyufeng1991/ReverseLinkedList_HeadNode.

The core code is as follows:

/**
 * The  whole idea is to take out a node, and then use the head interpolation method to create a new list, the new linked list is in reverse order.
 */
Node *reverselist (node *pnode) {

    node *reverselist;
    Initiallist (&reverselist);//The linked table Node *pmove is initialized in reverse order

    ;
    Node *pinsert;
    Pinsert = (node*) malloc (sizeof (Node));
    memset (pinsert, 0, sizeof (Node));
    Pinsert->next = NULL;
    Pmove = pnode->next;
    while (Pmove!= NULL) {

        //header interpolation
        pinsert->element = pmove->element;
        Pinsert->next = reverselist->next;
        Reverselist->next = Pinsert;

        Insert node reallocation memory
        Pinsert = (node*) malloc (sizeof (node));
        memset (pinsert, 0, sizeof (Node));
        Pinsert->next = NULL;

        Pmove = pmove->next;
    }

    printf ("%s function execution, reverse order lead node's single linked list creation succeeded \ n", __function__);
    Printlist (reverselist);

    return reverselist;
}


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.