Linked List Exercise [3]

The study arrangement is based on "Code Caprice"~leetcode24

At first glance, there is an idea, but not completely.

First of all, the head nodes in Likou all store useful values ​​to avoid unnecessary trouble, so we analyzed a wave:

The first step must be to create a dummy node and point to the head node.

The second step is singly linked list, the pointer can point to the same node at the same time, but a node can only have a pointer to the next node.

The third step changes the pointing of the pointer, and the naming of the pointer changes.

The fourth step is the termination condition and the loop condition.

The four steps are established, but when you are ready to write, you are stumped.

How do I find the next pointer after changing the pointer to point to?

How are the loop conditions established?

After thinking for a while, combined with other people's ideas, I wrote the code again out of the script to deepen my understanding.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) 
    {
        ListNode*dummy=new ListNode(0);
        dummy->next=head;
        ListNode*cur=dummy;
        while(cur->next!=NULL&&cur->next->next!=nullptr)
        {
            
            ListNode*temp1=cur->next;
            ListNode*temp2=cur->next->next->next;
             
            cur->next=cur->next->next;
            cur->next->next=temp1;
            cur->next->next->next=temp2;

            cur=cur->next->next;
        }
        return dummy->next;

    }
};

About the first three steps:

ListNode*dummy=new ListNode(0);
dummy->next=head;
ListNode*cur=dummy;

Individuals will also write, which is relatively simple and will not be repeated.

Regarding termination conditions:

 while(cur->next!=NULL&&cur->next->next!=nullptr)

Comparing what I wrote myself, it is also correct, but the ideas are not exactly the same, and the loop will be used later.

About the loop body:

ListNode*temp1=cur->next; 
ListNode*temp2=cur->next->next->next;
cur->next=cur->next->next;
cur->next->next=temp1;
cur->next->next->next=temp2;

The first two steps use a temporary pointer to point to the address of the node that may change, in case the node cannot be found after changing the pointer.

The last three steps are the first time to change the node position.

Summary: You can temporarily set the pointer to facilitate the changed node addressing! ! ! !

Regarding the loop condition:

cur=cur->next->next;

After one loop, reset the dummy node to the previous node at the first node that only wants to change its position.

If the node you want to change has only one bit, then cur->next->next=nullptr

If no node needs to be shifted, the dummy node's cur->next=nullptr.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324064675&siteId=291194637