Skip to content

Commit ea1335f

Browse files
committed
添加py,提供另一种解法
1 parent b48730d commit ea1335f

File tree

1 file changed

+76
-18
lines changed

1 file changed

+76
-18
lines changed

animation-simulation/链表篇/leetcode206反转链表.md

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@
2525

2626
则我们将 temp 指针指向 low 节点,此时则完成了反转。
2727

28-
反转之后我们继续反转下一节点,则
28+
反转之后我们继续反转下一节点,则 low = temp 即可。然后重复执行上诉操作直至最后,这样则完成了反转链表。
2929

30-
low = temp 即可。然后重复执行上诉操作直至最后,这样则完成了反转链表。
31-
32-
我们下面看代码吧
30+
我们下面看代码吧。
3331

3432
我会对每个关键点进行注释,大家可以参考动图理解。
3533

@@ -41,15 +39,10 @@ Java Code:
4139
```java
4240
class Solution {
4341
public ListNode reverseList(ListNode head) {
44-
//特殊情况
42+
//特殊情况
4543
if (head == null || head.next == null) {
4644
return head;
4745
}
48-
//反转
49-
return reverse(head);
50-
}
51-
public ListNode reverse (ListNode head) {
52-
5346
ListNode low = null;
5447
ListNode pro = head;
5548
while (pro != null) {
@@ -64,42 +57,41 @@ class Solution {
6457
}
6558
return low;
6659
}
67-
6860
}
6961
```
7062
JS Code:
7163
```javascript
7264
var reverseList = function(head) {
65+
//特殊情况
7366
if(!head || !head.next) {
7467
return head;
7568
}
7669
let low = null;
7770
let pro = head;
7871
while (pro) {
72+
//代表橙色指针
7973
let temp = pro;
74+
//移动绿色指针
8075
pro = pro.next;
76+
//反转节点
8177
temp.next = low;
78+
//移动黄色指针
8279
low = temp;
8380
}
8481
return low;
8582
};
8683
```
8784

88-
C++代码
85+
C++ Code:
8986

9087
```cpp
9188
class Solution {
9289
public:
9390
ListNode* reverseList(ListNode* head) {
94-
//特殊情况
91+
//特殊情况
9592
if (head == nullptr || head->next == nullptr) {
9693
return head;
9794
}
98-
//反转
99-
return reverse(head);
100-
}
101-
ListNode * reverse (ListNode * head) {
102-
10395
ListNode * low = nullptr;
10496
ListNode * pro = head;
10597
while (pro != nullptr) {
@@ -117,6 +109,28 @@ public:
117109
};
118110
```
119111
112+
Python Code:
113+
114+
```py
115+
class Solution:
116+
def reverseList(self, head: ListNode) -> ListNode:
117+
//特殊情况
118+
if head is None or head.next is None:
119+
return head
120+
low = None
121+
pro = head
122+
while pro is not None:
123+
# 代表橙色指针
124+
temp = pro
125+
# 移动绿色指针
126+
pro = pro.next
127+
# 反转节点
128+
temp.next = low
129+
# 移动黄色指针
130+
low = temp
131+
return low
132+
```
133+
120134
上面的迭代写法是不是搞懂啦,现在还有一种递归写法,不是特别容易理解,刚开始刷题的同学,可以只看迭代解法。
121135

122136

@@ -148,11 +162,17 @@ class Solution {
148162
JS Code:
149163
```javascript
150164
var reverseList = function(head) {
165+
//结束条件
151166
if (!head || !head.next) {
152167
return head;
153168
}
169+
//保存最后一个节点
154170
let pro = reverseList(head.next);
171+
//将节点进行反转。我们可以这样理解 4.next.next = 4;
172+
//4.next = 5;
173+
//则 5.next = 4 则实现了反转
155174
head.next.next = head;
175+
//防止循环
156176
head.next = null;
157177
return pro;
158178
};
@@ -181,3 +201,41 @@ public:
181201
};
182202
```
183203
204+
Python Code:
205+
206+
```py
207+
class Solution:
208+
def reverseList(self, head: ListNode) -> ListNode:
209+
# 结束条件
210+
if head is None or head.next is None:
211+
return head
212+
# 保存最后一个节点
213+
pro = self.reverseList(head.next)
214+
# 将节点进行反转。我们可以这样理解 4->next->next = 4;
215+
# 4->next = 5;
216+
# 则 5->next = 4 则实现了反转
217+
head.next.next = head
218+
# 防止循环
219+
head.next = None
220+
return pro
221+
```
222+
223+
<br/>
224+
225+
> 贡献者[@jaredliw](https://github.com/jaredliw)注:
226+
>
227+
> 这里提供一个比较直观的递归写法供大家参考。
228+
>
229+
> ```py
230+
> class Solution:
231+
> def reverseList(self, head: ListNode, prev_nd: ListNode = None) -> ListNode:
232+
> # 结束条件
233+
> if head is None:
234+
> return prev_nd
235+
> # 记录下一个节点并反转
236+
> next_nd = head.next
237+
> head.next = prev_nd
238+
> # 给定下一组该反转的节点
239+
> return self.reverseList(next_nd, head)
240+
> ```
241+

0 commit comments

Comments
 (0)