Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 54c8a48

Browse files
添加第64题
1 parent d4024ae commit 54c8a48

File tree

4 files changed

+192
-0
lines changed

4 files changed

+192
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* @lc app=leetcode id=92 lang=cpp
3+
*
4+
* [92] Reverse Linked List II
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* struct ListNode {
11+
* int val;
12+
* ListNode *next;
13+
* ListNode() : val(0), next(nullptr) {}
14+
* ListNode(int x) : val(x), next(nullptr) {}
15+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
16+
* };
17+
*/
18+
class Solution {
19+
public:
20+
// 递归法
21+
ListNode* reverseBetween(ListNode* head, int left, int right) {
22+
if (left == 1)
23+
// 如果从表头开始反转,相当于反转前N个节点
24+
return reverseN(head, right);
25+
// 对于 head->next 相当于反转区间为 [left - 1, right - 1]
26+
head->next = reverseBetween(head->next, left - 1, right - 1);
27+
return head;
28+
}
29+
30+
ListNode* right_plus;
31+
32+
ListNode* reverseN(ListNode* head, int right) {
33+
if (right == 1)
34+
{
35+
right_plus = head->next;
36+
return head;
37+
}
38+
ListNode* last = reverseN(head->next, right - 1);
39+
head->next->next = head;
40+
// head->next = nullptr; // 全链表翻转时
41+
head->next = right_plus;
42+
return last;
43+
}
44+
45+
// 头插法
46+
ListNode* reverseBetween2(ListNode* head, int left, int right) {
47+
ListNode *dummy;
48+
dummy->next = head;
49+
ListNode *g = dummy, *p = dummy->next;
50+
51+
for (size_t i = 0; i < left; i++)
52+
{
53+
g = g->next;
54+
p = p->next;
55+
}
56+
for (size_t i = 0; i < right - left; i++)
57+
{
58+
ListNode *tmp = p->next;
59+
p->next = p->next->next;
60+
tmp->next = g->next;
61+
g->next = tmp;
62+
}
63+
return dummy->next;
64+
}
65+
};
66+
// @lc code=end
67+
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# 反转链表 II
2+
3+
#### *Reverse Linked List II*
4+
5+
给你单链表的头指针 `head` 和两个整数 `left``right` ,其中 `left <= right` 。请你反转从位置 `left` 到位置 `right` 的链表节点,返回 反转后的链表 。
6+
7+
提示:
8+
9+
给定链表的结点数介于 1 和 100 之间。
10+
11+
12+
英文题目:
13+
14+
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*.
15+
16+
**Constraints:**
17+
18+
- The number of nodes in the list is `n`.
19+
- `1 <= n <= 500`
20+
- `-500 <= Node.val <= 500`
21+
- `1 <= left <= right <= n`
22+
23+
**example 1**
24+
25+
![example 1](https://github.com/SherlockUnknowEn/leetcode/blob/master/60-69/64.%20Reverse%20Linked%20List%20II(Medium)/rev2ex2.jpg)
26+
27+
```
28+
Input: head = [1,2,3,4,5], left = 2, right = 4
29+
Output: [1,4,3,2,5]
30+
```
31+
32+
**example 2**
33+
34+
```
35+
Input: head = [5], left = 1, right = 1
36+
Output: [5]
37+
```
38+
39+
40+
41+
---
42+
43+
### 思路
44+
45+
1. 递归法:为`head`节点编号为1时,反转`[left, right]`之间的节点。当第二个节点(`head->next`)编号为1时,题目相当于反转`[left-1, right-1]`之间的节点。当第`left`个节点编号为1时,题目相当于反转**`right - left`**节点。
46+
2. 反转前`N`个节点的算法,由`reverseN(ListNode* head, int N)`函数给出
47+
3. 头插法:设`g``left`左边的节点,`p``left`节点移动到`right`,`p`遍历过程使用**头插法**插入到`g`后,即可实现倒序
48+
49+
50+
### 代码
51+
```cpp
52+
53+
/*
54+
* @lc app=leetcode id=92 lang=cpp
55+
*
56+
* [92] Reverse Linked List II
57+
*/
58+
59+
// @lc code=start
60+
/**
61+
* Definition for singly-linked list.
62+
* struct ListNode {
63+
* int val;
64+
* ListNode *next;
65+
* ListNode() : val(0), next(nullptr) {}
66+
* ListNode(int x) : val(x), next(nullptr) {}
67+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
68+
* };
69+
*/
70+
class Solution {
71+
public:
72+
// 递归法
73+
ListNode* reverseBetween(ListNode* head, int left, int right) {
74+
if (left == 1)
75+
// 如果从表头开始反转,相当于反转前N个节点
76+
return reverseN(head, right);
77+
// 对于 head->next 相当于反转区间为 [left - 1, right - 1]
78+
head->next = reverseBetween(head->next, left - 1, right - 1);
79+
return head;
80+
}
81+
82+
ListNode* N1_node;
83+
84+
ListNode* reverseN(ListNode* head, int N) {
85+
if (N == 1)
86+
{
87+
N1_node = head->next;
88+
return head;
89+
}
90+
ListNode* last = reverseN(head->next, N - 1);
91+
head->next->next = head;
92+
// head->next = nullptr; // 全链表翻转时,原先的头节点变为尾节点,next指向null
93+
head->next = N1_node; // 反转前N个节点时,原先的头结点next指向第 N+1 个
94+
return last;
95+
}
96+
97+
// 头插法
98+
ListNode* reverseBetween2(ListNode* head, int left, int right) {
99+
ListNode *dummy;
100+
dummy->next = head;
101+
ListNode *g = dummy, *p = dummy->next;
102+
103+
for (size_t i = 0; i < left; i++)
104+
{
105+
g = g->next;
106+
p = p->next;
107+
}
108+
for (size_t i = 0; i < right - left; i++)
109+
{
110+
ListNode *tmp = p->next;
111+
p->next = p->next->next;
112+
tmp->next = g->next;
113+
g->next = tmp;
114+
}
115+
return dummy->next;
116+
}
117+
};
118+
// @lc code=end
119+
120+
```
121+
122+
本题以及其它leetcode题目代码github地址: [github地址](https:github.com/SherlockUnknowEn/leetcode)
19.4 KB
Loading[フレーム]

‎README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,6 @@
6161
##### 61. [142.Linked List Cycle II](https://github.com/SherlockUnknowEn/leetcode/tree/master/60-69/61.%20Linked%20List%20Cycle%20II(Medium)) 环形链表 II
6262
##### 62. [160.Intersection of Two Linked Lists](https://github.com/SherlockUnknowEn/leetcode/tree/master/60-69/62.%20Intersection%20of%20Two%20Linked%20Lists(Easy)) 相交链表
6363
##### 63. [876.Middle of the Linked List](https://github.com/SherlockUnknowEn/leetcode/tree/master/60-69/63.%20Middle%20of%20the%20Linked%20List(Easy)) 链表的中间结点
64+
65+
##### 64. [92.Reverse Linked List II](https://github.com/SherlockUnknowEn/leetcode/tree/master/60-69/64.%20Reverse%20Linked%20List%20II(Medium)) 反转链表 II
66+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /