Reverse list & exchange list node

Posted by BenGilbert on Sat, 02 Nov 2019 09:22:35 +0100

leetcode list reverse link

Summary of requirements:

Invert a single chain table

Input: 1 - > 2 - > 3 - > 4 - > 5 - > null

Output: 5 - > 4 - > 3 - > 2 - > 1 - > null

The approach is to consider the next of the node in the linked list to point to its previous node, so as to reverse.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* reverseList(struct ListNode* head){
    if(head==NULL)
        return NULL;
    struct ListNode* pre=NULL;
    struct ListNode* p=head;
    struct ListNode* h=NULL;
    while(p)
    {
        h=p;          //h is constantly changing, and finally is the head node of the reverse linked list.
        struct ListNode* tmp=p->next; //tmp continuously points to the next node for traversing the linked list  
        p->next=pre;    //The pointer domain of p node is changed from the next node to the previous node.
        pre=p;     //Set pre as the current node. When a node is taken down, pre is the previous node of the next node.
        p=tmp;             //That is, P = P - > next, to continuously remove a node
    }
The nodes in two exchange linked list

Summary of requirements:

Given a linked list, two pairs exchange the adjacent nodes and return the linked list after exchange.

You can't just change the internal values of nodes, but you need to actually exchange nodes.

Example: given 1 - > 2 - > 3 - > 4, you should return 2 - > 1 - > 4 - > 3.

Practice:

Each operation needs to know three nodes: tmp, pre and tail. It can be regarded as a group of three nodes. Among them, tmp, the previous node of the current node pre of this group, is the last node tail of the previous group, and then exchange the pre and tail of this group.

Move to the next node or group:

Schematic diagram of specific node exchange operation steps:

Full code:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


truct ListNode* swapPairs(struct ListNode* head){
    //Null if NULL; only one node returns as is
    if(head==NULL)
        return NULL;
    if(head->next==NULL)
        return head;

    struct ListNode* tmp=NULL;  //The function is to save the last node after a cycle
    struct ListNode* pre=head;
    struct ListNode* tail=NULL;

    tail=head->next; //The primary node exchange operation of advanced line is to determine the header node
    //Node exchange
    pre->next=tail->next;
    tail->next=pre;
    head=tail;
    tmp=pre;  //Save last node

    pre=pre->next; //The current node moves backward for the next operation
    if(pre==NULL || pre->next==NULL) //If the list has only two or three nodes, return the list
        return head;

    while(pre && pre->next)
    {
        tail=pre->next;
        //Node exchange
        pre->next=tail->next;
        tail->next=pre;
        tmp->next=tail;
        tmp=pre; //Save last node

        pre=pre->next;
    }

    return head;
}

In addition, a very simple code is attached:

ListNode *swapPairs(ListNode *head) {
        if (!head) return NULL;
        if (!head->next) return head;
        ListNode *temp = head->next;
        head->next = swapPairs(temp->next);
        temp->next = head;
        return temp;
    }

Original post of this Code: https://blog.csdn.net/stay_the_course/article/details/88729356

Topics: PHP