LeetCode 92. Reverse Linked List II [linked list, head insertion method] medium

This article belongs to one of the series of articles "Conquering LeetCode". This series officially started on 2021/08/12. Since some questions on LeetCode are locked, this series will continue at least until all unlocked questions are cleared; since LeetCode is still creating new questions, the end date of this series may be forever. In this series of problem-solving articles, I will not only explain a variety of problem-solving ideas and their optimization, but also use a variety of programming languages ​​to solve the problems. When it comes to general solution methods, I will also summarize the corresponding algorithm templates.

In order to facilitate running, debugging and sharing code files on PC, I also established a related warehouse: https://github.com/memcpy0/LeetCode-Conquest . In this warehouse, you can not only see the link to the original LeetCode question, solution code, link to the solution article, summary of similar questions, summary of common solutions, etc., but also important information such as the frequency of original questions and related companies. If you have other preferred solutions, you can share them with others.

Since the content of this series of articles may be updated and changed at any time, you are welcome to follow and save the article Table of Contents of the Conquering LeetCode series of articles as a reminder.

You are given the head pointer of a singly linked list  head and the  left sum of  two integers right , where  left <= right . Please reverse the linked list nodes from position  left to position  right and return  the reversed linked list  .

Example 1:

输入:head = [1,2,3,4,5], left = 2, right = 4
输出:[1,4,3,2,5]

Example 2:

输入:head = [5], left = 1, right = 1
输出:[5]

hint:

  • The number of nodes in the linked list is n
  • 1 <= n <= 500
  • -500 <= Node.val <= 500
  • 1 <= left <= right <= n

Advanced:  Can you complete the reversal using one sweep?


The solution is to traverse the "threading the needle" to reverse the linked list in one go


How to achieve this is explained in detail below. Use pointer variables to record the variables needed during the reversal process. Their meanings are as follows:

  • curr: Points to the first node of the area to be reversed ;
  • next: always points currto the next node of , during the loop process, will change currafter the change ;next
  • p0: Always points to the leftprevious node of the first node in the area to be reversed, and remains unchanged during the loop .
  • prev: Always points to the previous node of the inverted area , immediately to currthe left.

Steps:

  1. Find p0, order prev = nullptr, curr = p0->next.
  2. Enter the loop:
    1. First currrecord the next node as next;
    2. Execute operation ①: point curr.nextto the previous node prev;
    3. Execute operation ②: point prevto the current node curr;
    4. Execute operation ③: Move currto the next node next.
  3. After exceeding the area to be reversed, p0->nextpoint to the end of the reversed area, set p0->next->nextto curr, and then set p0->nextto prev.
class Solution {
    
    
public:
    ListNode* reverseBetween(ListNode* head, int left, int right) {
    
    
        // 设置 dummy 是此类问题的一般做法
        ListNode* dummy = new ListNode(0, head), *p0 = dummy;
        for (int i = 0; i < left - 1; ++i) p0 = p0->next;

        ListNode *prev = nullptr, *curr = p0->next;
        for (int i = 0; i < right - left + 1; ++i) {
    
    
            ListNode *next = curr->next;
            curr->next = prev;
            prev = curr;
            curr = next;
        }
        p0->next->next = curr;
        p0->next = prev;
        return dummy->next;
    }
};

Complexity analysis:

  • Time complexity: O ( n ) \mathcal{O}(n)O ( n ) , of whichnnn is the number of linked list nodes.
  • Space complexity: O ( 1 ) \mathcal{O}(1)O ( 1 ) , using only a few extra variables.

Guess you like

Origin blog.csdn.net/myRealization/article/details/132749940