@@ -43,7 +43,6 @@ Java Code:
43
43
``` java
44
44
class Solution {
45
45
public ListNode reverseBetween (ListNode head , int left , int right ) {
46
-
47
46
// 虚拟头节点
48
47
ListNode temp = new ListNode (- 1 );
49
48
temp. next = head;
@@ -53,26 +52,28 @@ class Solution {
53
52
for (; i < left- 1 ; ++ i) {
54
53
pro = pro. next;
55
54
}
56
- // 保存 left 节点前的第一个节点
55
+ // 保存 left 节点前的一个节点
57
56
ListNode leftNode = pro;
57
+ // 来到 right 节点
58
58
for (; i < right; ++ i) {
59
59
pro = pro. next;
60
60
}
61
- // 保存 right 节点后的节点
61
+ // 保存 right 节点后的一个节点
62
62
ListNode rightNode = pro. next;
63
63
// 切断链表
64
- pro. next = null ;
65
- ListNode newhead = leftNode. next;
66
- leftNode. next = null ;
67
- leftNode. next = rever(newhead);
64
+ pro. next = null ;// 切断 right 后的部分
65
+ ListNode newhead = leftNode. next;// 保存 left 节点
66
+ leftNode. next = null ;// 切断 left 前的部分
67
+ // 反转
68
+ leftNode. next = reverse(newhead);
68
69
// 重新接头
69
70
newhead. next = rightNode;
70
71
return temp. next;
71
72
72
73
}
73
74
// 和反转链表1代码一致
74
- public ListNode rever (ListNode head ) {
75
- ListNode low = null ;
75
+ public ListNode reverse (ListNode head ) {
76
+ ListNode low = null ;
76
77
ListNode pro = head;
77
78
while (pro != null ) {
78
79
ListNode temp = pro;
@@ -91,6 +92,7 @@ C++ Code:
91
92
class Solution {
92
93
public:
93
94
ListNode* reverseBetween(ListNode* head, int left, int right) {
95
+ //虚拟头节点
94
96
ListNode * temp = new ListNode(-1);
95
97
temp->next = head;
96
98
ListNode * pro = temp;
@@ -99,23 +101,26 @@ public:
99
101
for (; i < left-1; ++i) {
100
102
pro = pro->next;
101
103
}
102
- // 保存 left 节点前的第一个节点
104
+ //保存 left 节点前的一个节点
103
105
ListNode * leftNode = pro;
106
+ //来到 right 节点
104
107
for (; i < right; ++i) {
105
108
pro = pro->next;
106
109
}
107
- // 保存 right 节点后的节点
110
+ //保存 right 节点后的一个节点
108
111
ListNode * rightNode = pro->next;
109
112
//切断链表
110
- pro->next = nullptr;
111
- ListNode * newhead = leftNode->next;
112
- leftNode->next = nullptr;
113
- leftNode->next = rever(newhead);
113
+ pro->next = nullptr;//切断 right 后的部分
114
+ ListNode * newhead = leftNode->next;//保存 left 节点
115
+ leftNode->next = nullptr;//切断 left 前的部分
116
+ //反转
117
+ leftNode->next = reverse(newhead);
114
118
//重新接头
115
119
newhead->next = rightNode;
116
120
return temp->next;
117
121
}
118
- ListNode * rever (ListNode * head) {
122
+ //和反转链表1代码一致
123
+ ListNode * reverse (ListNode * head) {
119
124
ListNode * low = nullptr;
120
125
ListNode * pro = head;
121
126
while (pro != nullptr) {
@@ -129,3 +134,90 @@ public:
129
134
};
130
135
```
131
136
137
+ JS Code:
138
+
139
+ ```js
140
+ var reverseBetween = function(head, left, right) {
141
+ //虚拟头节点
142
+ let temp = new ListNode(-1);
143
+ temp.next = head;
144
+ let pro = temp;
145
+ //来到 left 节点前的一个节点
146
+ let i = 0;
147
+ for (; i < left-1; ++i) {
148
+ pro = pro.next;
149
+ }
150
+ //保存 left 节点前的一个节点
151
+ let leftNode = pro;
152
+ //来到 right 节点
153
+ for (; i < right; ++i) {
154
+ pro = pro.next;
155
+ }
156
+ //保存 right 节点后的一个节点
157
+ let rightNode = pro.next;
158
+ //切断链表
159
+ pro.next = null;//切断 right 后的部分
160
+ let newhead = leftNode.next;//保存 left 节点
161
+ leftNode.next = null;//切断 left 前的部分
162
+ //反转
163
+ leftNode.next = reverse(newhead);
164
+ //重新接头
165
+ newhead.next = rightNode;
166
+ return temp.next;
167
+ };
168
+
169
+
170
+ //和反转链表1代码一致
171
+ var reverse = function(head) {
172
+ let low = null;
173
+ let pro = head;
174
+ while (pro) {
175
+ let temp = pro;
176
+ pro = pro.next;
177
+ temp.next = low;
178
+ low = temp;
179
+ }
180
+ return low;
181
+ };
182
+ ```
183
+
184
+ Python Code:
185
+
186
+ ``` py
187
+ class Solution :
188
+ def reverseBetween (self , head : ListNode, left : int , right : int ) -> ListNode:
189
+ # 虚拟头节点
190
+ temp = ListNode(- 1 )
191
+ temp.next = head
192
+ pro = temp
193
+ # 来到 left 节点前的一个节点
194
+ for _ in range (left - 1 ):
195
+ pro = pro.next
196
+ # 保存 left 节点前的第一个节点
197
+ leftNode = pro
198
+ for _ in range (right - left + 1 ):
199
+ pro = pro.next
200
+ # 保存 right 节点后的节点
201
+ rightNode = pro.next
202
+ # 切断链表
203
+ pro.next = None # 切断 right 后的部分
204
+ newhead = leftNode.next # 保存 left 节点
205
+ leftNode.next = None # 切断 left 前的部分
206
+ # 反转
207
+ leftNode.next = self .reverse(newhead)
208
+ # 重新接头
209
+ newhead.next = rightNode
210
+ return temp.next
211
+
212
+ # 和反转链表1代码一致
213
+ def reverse (self , head ):
214
+ low = None
215
+ pro = head
216
+ while pro is not None :
217
+ temp = pro
218
+ pro = pro.next
219
+ temp.next = low
220
+ low = temp
221
+ return low
222
+ ```
223
+
0 commit comments