LeetCode_LinkedList_92. Reverse Linked List II (C++/Java) [recursion]

Posted by taz321 on Sun, 23 Jan 2022 04:30:06 +0100

catalogue

1, Title Description

English description

Chinese description

2, Problem solving ideas

3, AC code

C++

Java

4, Problem solving process

First Bo

 

1, Title Description

English description

Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.

Example 1:


Example 2:

Constraints:

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

Follow up: Could you do it in one pass?

Chinese description

Give you the head pointer of the single linked list and the two integers "left" and right, where "left" < = right. Please reverse the linked list node from position left to position right and return the reversed linked list.
 

Example 1:


Example 2:

Tips:

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

Source: LeetCode
Link: https://leetcode-cn.com/problems/reverse-linked-list-ii
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

2, Problem solving ideas

First consider a problem: how to reverse the first N nodes of the linked list

  1. One method is to use the head insertion method (iteration), record the number of nodes inserted into the head, and stop when it reaches N. In addition, two positions need to be recorded: the head node and the next node to be inserted after the head node. (since the operation is performed in the original linked list, you should be extra careful about the stripping and reinsertion of nodes, and be sure to ensure the unique directivity of the whole linked list.)
  2. One is recursive, which only needs to ensure that the number of inversion nodes is N.

It can be seen from the above that the recursive method is easier to implement, and there is basically no need to add anything else, although the recursive implementation will perform poorly in memory consumption. (you need to keep pushing variables onto the stack)

Now that we have the method of reversing the N nodes before, then we only need to locate the header node and then call the algorithm to reverse the N node.

Since the linked list may be reversed from the head node, you need to add a virtual head node.

In which case do you need to add a virtual head node?

A simple skill, when the head node may change, you can add a virtual head node to simplify the algorithm to ensure that there is always a stable beginning, so as to avoid all kinds of possible boundary problems. For example, it is possible to insert, delete, etc. in the header.

3, AC code

Only the specific code implementation of recursive algorithm is given here.

C++

/**
 * 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* ReverseN(ListNode* head, int n) {
        if(n == 0 || n == 1) return head;
        ListNode* tail = head->next, * h = ReverseN(head->next, n - 1);
        head->next = tail->next;
        tail->next = head;
        return h;
    }
    ListNode* reverseBetween(ListNode* head, int left, int right) {
        int cnt = right - left + 1;
        ListNode* h = new ListNode, * tem = h;
        h->next = head;
        while(--left) tem = tem->next;
        tem->next = ReverseN(tem->next, cnt);
        return h->next;
    }
};

Java

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseN(ListNode head, int n) {
        if(n == 0 || n == 1) return head;
        ListNode tail = head.next;
        ListNode h = reverseN(head.next, n - 1);
        head.next = tail.next;
        tail.next = head;
        return h;
    }
    public ListNode reverseBetween(ListNode head, int left, int right) {
        int cnt = right - left + 1;
        ListNode h = new ListNode();
        h.next = head;
        ListNode tem = h;
        while(--left > 0) tem = tem.next;
        tem.next = reverseN(tem.next, cnt);
        return h.next;
    }
}

4, Problem solving process

First Bo

After understanding the algorithm, it is easy to solve it. As soon as it is sent into the soul

Topics: leetcode recursion