[27] Pairwise exchange of nodes in the linked list | Rotating linked list (LeetCode 24 | 61)

Pairwise swap nodes in the linked list

Problem Description

Given a linked list, exchange adjacent nodes in it in pairs, and return the exchanged linked list.

You can't just simply change the internal value of the node, but need to actually exchange the node.

Problem-solving ideas

Add a new head node to assist in solving the problem, draw the exchange situation when there are three nodes, and then use the code to achieve:
Insert picture description here
as above, the black handwriting is the original linked list, the blue avoidance is to draw the pointers after the exchange, and the red handwriting It is a pseudo code written in blue handwriting.

/**
 * 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* pre_head = new ListNode(0,head);
        ListNode* cur = pre_head;
        while(cur->next && cur->next->next){
    
    
            ListNode* p = cur->next;
            ListNode* q = p->next;
            cur->next = q;
            p->next = q->next;
            q->next = p;
            cur = p;
        }
        return pre_head->next;
    }
};

Time complexity: O(n)
Space complexity: O(1)

Recursive ideas provided by the official solution:

The termination condition of recursion is that there is no node in the linked list, or there is only one node in the linked list.

If there are at least two nodes in the linked list, the next node of the first node points to the linked list originally connected to the second node, and the next node of the second node points to the first node.

Illustration:
Insert picture description here

class Solution {
    
    
public:
    ListNode* swapPairs(ListNode* head) {
    
    
        if (head == nullptr || head->next == nullptr) {
    
    
            return head;
        }
        ListNode* newHead = head->next;
        head->next = swapPairs(newHead->next);
        newHead->next = head;
        return newHead;
    }
};

Rotating linked list

Problem Description

Given a linked list, rotate the linked list and move each node of the linked list k positions to the right, where k is a non-negative number.

Example:
Insert picture description here

Problem-solving ideas

According to the example, the first reaction is to loop k times, and move each node one bit back one by one each time. The second reaction thinks that this is a bit expensive, so I thought of converting the singly linked list to a circular linked list, positioning the n-(k%n) node to the new head node, and then converting the circular linked list to a singly linked list. .

class Solution {
    
    
public:
    ListNode* rotateRight(ListNode* head, int k) {
    
    
        if(!head ) return head;
        ListNode* cur = head;
        int n = 1;
        while(cur->next){
    
     //求出链表长度
            n++;
            cur = cur->next;
        }
        cur->next = head; //改成循环链表
        k = n-(k%n);//因为是向右移动,所以需要对k做处理
        for(int i=0;i<k;i++) //将头结点变为第k个
            head = head->next;
        cur = head;
        for(int i=0;i<n-1;i++) //恢复成单链表
            cur = cur->next;
        cur->next = nullptr;
        return head;
    }
};

Time complexity: O(n)
Space complexity: O(1)

After reading the official solution, the idea is the same.

Guess you like

Origin blog.csdn.net/qq_43424037/article/details/113429571