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 pointscurr
to the next node of , during the loop process, will changecurr
after the change ;next
p0
: Always points to theleft
previous 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 tocurr
the left.
Steps:
- Find
p0
, orderprev = nullptr, curr = p0->next
. - Enter the loop:
- First
curr
record the next node asnext
; - Execute operation ①: point
curr.next
to the previous nodeprev
; - Execute operation ②: point
prev
to the current nodecurr
; - Execute operation ③: Move
curr
to the next nodenext
.
- First
- After exceeding the area to be reversed,
p0->next
point to the end of the reversed area, setp0->next->next
tocurr
, and then setp0->next
toprev
.
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.