Skip to content

Commit cbca4b1

Browse files
committed
feat: add solutions to lc problem: No.0092
No.0092.Reverse Linked List II
1 parent 1e065ab commit cbca4b1

File tree

8 files changed

+200
-109
lines changed

8 files changed

+200
-109
lines changed

solution/0000-0099/0092.Reverse Linked List II/README.md

Lines changed: 85 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46+
**方法一:模拟**
47+
48+
定义一个虚拟头结点 `dummy`,指向链表的头结点 `head`,然后定义一个指针 `pre` 指向 `dummy`,从虚拟头结点开始遍历链表,遍历到第 `left` 个结点时,将 `pre` 指向该结点,然后从该结点开始遍历 `right - left + 1` 次,将遍历到的结点依次插入到 `pre` 的后面,最后返回 `dummy.next` 即可。
49+
50+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为链表的长度。
51+
4652
<!-- tabs:start -->
4753

4854
### **Python3**
@@ -56,8 +62,8 @@
5662
# self.val = val
5763
# self.next = next
5864
class Solution:
59-
def reverseBetween(self, head: ListNode, left: int, right: int) -> ListNode:
60-
if head is None or head.next is None or left == right:
65+
def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
66+
if head.next is None or left == right:
6167
return head
6268
dummy = ListNode(0, head)
6369
pre = dummy
@@ -91,7 +97,7 @@ class Solution:
9197
*/
9298
class Solution {
9399
public ListNode reverseBetween(ListNode head, int left, int right) {
94-
if (head == null || head.next == null || left == right) {
100+
if (head.next == null || left == right) {
95101
return head;
96102
}
97103
ListNode dummy = new ListNode(0, head);
@@ -115,6 +121,78 @@ class Solution {
115121
}
116122
```
117123

124+
### **C++**
125+
126+
```cpp
127+
/**
128+
* Definition for singly-linked list.
129+
* struct ListNode {
130+
* int val;
131+
* ListNode *next;
132+
* ListNode() : val(0), next(nullptr) {}
133+
* ListNode(int x) : val(x), next(nullptr) {}
134+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
135+
* };
136+
*/
137+
class Solution {
138+
public:
139+
ListNode* reverseBetween(ListNode* head, int left, int right) {
140+
if (!head->next || left == right) {
141+
return head;
142+
}
143+
ListNode* dummy = new ListNode(0, head);
144+
ListNode* pre = dummy;
145+
for (int i = 0; i < left - 1; ++i) {
146+
pre = pre->next;
147+
}
148+
ListNode *p = pre, *q = pre->next;
149+
ListNode* cur = q;
150+
for (int i = 0; i < right - left + 1; ++i) {
151+
ListNode* t = cur->next;
152+
cur->next = pre;
153+
pre = cur;
154+
cur = t;
155+
}
156+
p->next = pre;
157+
q->next = cur;
158+
return dummy->next;
159+
}
160+
};
161+
```
162+
163+
### **Go**
164+
165+
```go
166+
/**
167+
* Definition for singly-linked list.
168+
* type ListNode struct {
169+
* Val int
170+
* Next *ListNode
171+
* }
172+
*/
173+
func reverseBetween(head *ListNode, left int, right int) *ListNode {
174+
if head.Next == nil || left == right {
175+
return head
176+
}
177+
dummy := &ListNode{0, head}
178+
pre := dummy
179+
for i := 0; i < left-1; i++ {
180+
pre = pre.Next
181+
}
182+
p, q := pre, pre.Next
183+
cur := q
184+
for i := 0; i < right-left+1; i++ {
185+
t := cur.Next
186+
cur.Next = pre
187+
pre = cur
188+
cur = t
189+
}
190+
p.Next = pre
191+
q.Next = cur
192+
return dummy.Next
193+
}
194+
```
195+
118196
### **JavaScript**
119197

120198
```js
@@ -132,7 +210,7 @@ class Solution {
132210
* @return {ListNode}
133211
*/
134212
var reverseBetween = function (head, left, right) {
135-
if (!head || !head.next || left == right) {
213+
if (!head.next || left == right) {
136214
return head;
137215
}
138216
const dummy = new ListNode(0, head);
@@ -155,45 +233,6 @@ var reverseBetween = function (head, left, right) {
155233
};
156234
```
157235

158-
### **C++**
159-
160-
```cpp
161-
/**
162-
* Definition for singly-linked list.
163-
* struct ListNode {
164-
* int val;
165-
* ListNode *next;
166-
* ListNode() : val(0), next(nullptr) {}
167-
* ListNode(int x) : val(x), next(nullptr) {}
168-
* ListNode(int x, ListNode *next) : val(x), next(next) {}
169-
* };
170-
*/
171-
class Solution {
172-
public:
173-
ListNode* reverseBetween(ListNode* head, int left, int right) {
174-
if (head == nullptr || head->next == nullptr || left == right) {
175-
return head;
176-
}
177-
ListNode* dummy = new ListNode(0, head);
178-
ListNode* pre = dummy;
179-
for (int i = 0; i < left - 1; ++i) {
180-
pre = pre->next;
181-
}
182-
ListNode *p = pre, *q = pre->next;
183-
ListNode* cur = q;
184-
for (int i = 0; i < right - left + 1; ++i) {
185-
ListNode* t = cur->next;
186-
cur->next = pre;
187-
pre = cur;
188-
cur = t;
189-
}
190-
p->next = pre;
191-
q->next = cur;
192-
return dummy->next;
193-
}
194-
};
195-
```
196-
197236
### **C#**
198237

199238
```cs
@@ -210,21 +249,18 @@ public:
210249
*/
211250
public class Solution {
212251
public ListNode ReverseBetween(ListNode head, int left, int right) {
213-
if (head == null || head.next == null || left == right)
214-
{
252+
if (head.next == null || left == right) {
215253
return head;
216254
}
217255
ListNode dummy = new ListNode(0, head);
218256
ListNode pre = dummy;
219-
for (int i = 0; i < left - 1; ++i)
220-
{
257+
for (int i = 0; i < left - 1; ++i) {
221258
pre = pre.next;
222259
}
223260
ListNode p = pre;
224261
ListNode q = pre.next;
225262
ListNode cur = q;
226-
for (int i = 0; i < right - left + 1; ++i)
227-
{
263+
for (int i = 0; i < right - left + 1; ++i) {
228264
ListNode t = cur.next;
229265
cur.next = pre;
230266
pre = cur;

solution/0000-0099/0092.Reverse Linked List II/README_EN.md

Lines changed: 79 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
# self.val = val
4848
# self.next = next
4949
class Solution:
50-
def reverseBetween(self, head: ListNode, left: int, right: int) -> ListNode:
51-
if head is None or head.next is None or left == right:
50+
def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
51+
if head.next is None or left == right:
5252
return head
5353
dummy = ListNode(0, head)
5454
pre = dummy
@@ -80,7 +80,7 @@ class Solution:
8080
*/
8181
class Solution {
8282
public ListNode reverseBetween(ListNode head, int left, int right) {
83-
if (head == null || head.next == null || left == right) {
83+
if (head.next == null || left == right) {
8484
return head;
8585
}
8686
ListNode dummy = new ListNode(0, head);
@@ -104,6 +104,78 @@ class Solution {
104104
}
105105
```
106106

107+
### **C++**
108+
109+
```cpp
110+
/**
111+
* Definition for singly-linked list.
112+
* struct ListNode {
113+
* int val;
114+
* ListNode *next;
115+
* ListNode() : val(0), next(nullptr) {}
116+
* ListNode(int x) : val(x), next(nullptr) {}
117+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
118+
* };
119+
*/
120+
class Solution {
121+
public:
122+
ListNode* reverseBetween(ListNode* head, int left, int right) {
123+
if (!head->next || left == right) {
124+
return head;
125+
}
126+
ListNode* dummy = new ListNode(0, head);
127+
ListNode* pre = dummy;
128+
for (int i = 0; i < left - 1; ++i) {
129+
pre = pre->next;
130+
}
131+
ListNode *p = pre, *q = pre->next;
132+
ListNode* cur = q;
133+
for (int i = 0; i < right - left + 1; ++i) {
134+
ListNode* t = cur->next;
135+
cur->next = pre;
136+
pre = cur;
137+
cur = t;
138+
}
139+
p->next = pre;
140+
q->next = cur;
141+
return dummy->next;
142+
}
143+
};
144+
```
145+
146+
### **Go**
147+
148+
```go
149+
/**
150+
* Definition for singly-linked list.
151+
* type ListNode struct {
152+
* Val int
153+
* Next *ListNode
154+
* }
155+
*/
156+
func reverseBetween(head *ListNode, left int, right int) *ListNode {
157+
if head.Next == nil || left == right {
158+
return head
159+
}
160+
dummy := &ListNode{0, head}
161+
pre := dummy
162+
for i := 0; i < left-1; i++ {
163+
pre = pre.Next
164+
}
165+
p, q := pre, pre.Next
166+
cur := q
167+
for i := 0; i < right-left+1; i++ {
168+
t := cur.Next
169+
cur.Next = pre
170+
pre = cur
171+
cur = t
172+
}
173+
p.Next = pre
174+
q.Next = cur
175+
return dummy.Next
176+
}
177+
```
178+
107179
### **JavaScript**
108180

109181
```js
@@ -121,7 +193,7 @@ class Solution {
121193
* @return {ListNode}
122194
*/
123195
var reverseBetween = function (head, left, right) {
124-
if (!head || !head.next || left == right) {
196+
if (!head.next || left == right) {
125197
return head;
126198
}
127199
const dummy = new ListNode(0, head);
@@ -144,45 +216,6 @@ var reverseBetween = function (head, left, right) {
144216
};
145217
```
146218

147-
### **C++**
148-
149-
```cpp
150-
/**
151-
* Definition for singly-linked list.
152-
* struct ListNode {
153-
* int val;
154-
* ListNode *next;
155-
* ListNode() : val(0), next(nullptr) {}
156-
* ListNode(int x) : val(x), next(nullptr) {}
157-
* ListNode(int x, ListNode *next) : val(x), next(next) {}
158-
* };
159-
*/
160-
class Solution {
161-
public:
162-
ListNode* reverseBetween(ListNode* head, int left, int right) {
163-
if (head == nullptr || head->next == nullptr || left == right) {
164-
return head;
165-
}
166-
ListNode* dummy = new ListNode(0, head);
167-
ListNode* pre = dummy;
168-
for (int i = 0; i < left - 1; ++i) {
169-
pre = pre->next;
170-
}
171-
ListNode *p = pre, *q = pre->next;
172-
ListNode* cur = q;
173-
for (int i = 0; i < right - left + 1; ++i) {
174-
ListNode* t = cur->next;
175-
cur->next = pre;
176-
pre = cur;
177-
cur = t;
178-
}
179-
p->next = pre;
180-
q->next = cur;
181-
return dummy->next;
182-
}
183-
};
184-
```
185-
186219
### **C#**
187220

188221
```cs
@@ -199,21 +232,18 @@ public:
199232
*/
200233
public class Solution {
201234
public ListNode ReverseBetween(ListNode head, int left, int right) {
202-
if (head == null || head.next == null || left == right)
203-
{
235+
if (head.next == null || left == right) {
204236
return head;
205237
}
206238
ListNode dummy = new ListNode(0, head);
207239
ListNode pre = dummy;
208-
for (int i = 0; i < left - 1; ++i)
209-
{
240+
for (int i = 0; i < left - 1; ++i) {
210241
pre = pre.next;
211242
}
212243
ListNode p = pre;
213244
ListNode q = pre.next;
214245
ListNode cur = q;
215-
for (int i = 0; i < right - left + 1; ++i)
216-
{
246+
for (int i = 0; i < right - left + 1; ++i) {
217247
ListNode t = cur.next;
218248
cur.next = pre;
219249
pre = cur;

solution/0000-0099/0092.Reverse Linked List II/Solution.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
class Solution {
1212
public:
1313
ListNode* reverseBetween(ListNode* head, int left, int right) {
14-
if (head == nullptr || head->next == nullptr || left == right) {
14+
if (!head->next || left == right) {
1515
return head;
1616
}
1717
ListNode* dummy = new ListNode(0, head);

0 commit comments

Comments
 (0)