Movatterモバイル変換


[0]ホーム

URL:


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

Commit7d3b01b

Browse files
author
Yan Wen
committed
update反转链表:添加python递归法的代码
1 parentfb21a57 commit7d3b01b

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

‎problems/0206.翻转链表.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class Solution {
142142
}
143143
```
144144

145-
Python
145+
Python迭代法
146146
```python
147147
#双指针
148148
# Definition for singly-linked list.
@@ -163,6 +163,32 @@ class Solution:
163163
return pre
164164
```
165165

166+
Python递归法:
167+
168+
```python
169+
# Definition for singly-linked list.
170+
# class ListNode:
171+
# def __init__(self, val=0, next=None):
172+
# self.val = val
173+
# self.next = next
174+
classSolution:
175+
defreverseList(self,head: ListNode) -> ListNode:
176+
177+
defreverse(pre,cur):
178+
ifnot cur:
179+
return pre
180+
181+
tmp= cur.next
182+
cur.next= pre
183+
184+
return reverse(cur,tmp)
185+
186+
return reverse(None,head)
187+
188+
```
189+
190+
191+
166192
Go:
167193

168194
```go

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp