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

[pull] main from itcharge:main #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 7 commits into AlgorithmAndLeetCode:main from itcharge:main
Jun 22, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update 0206. 反转链表.md
  • Loading branch information
杨世超 committed Jun 21, 2022
commit 57e73b466dabf3fc428ea27792bcde1e67c2d3f0
57 changes: 31 additions & 26 deletions Solutions/0206. 反转链表.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,25 @@

**要求**:将该单链表进行反转。可以迭代或递归地反转链表。

比如:
**说明**:

```
翻转前:
1->2->3->4->5->NULL
反转后:
5->4->3->2->1->NULL
- 链表中节点的数目范围是 $[0, 5000]$。
- $-5000 \le Node.val \le 5000$。

**示例**:

```Python
输入 head = [1,2,3,4,5]
输出 [5,4,3,2,1]

解释
翻转前 1->2->3->4->5->NULL
反转后 5->4->3->2->1->NULL
```

## 解题思路

### 思路 1. 迭代
### 思路 1:迭代

1. 使用两个指针 `cur` 和 `pre` 进行迭代。`pre` 指向 `cur` 前一个节点位置。初始时,`pre` 指向 `None`,`cur` 指向 `head`。

Expand All @@ -36,7 +43,22 @@

![](https://qcdn.itcharge.cn/images/20220111133639.png)

### 思路 2. 递归
### 思路 1:迭代代码

```Python
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
pre = None
cur = head
while cur != None:
next = cur.next
cur.next = pre
pre = cur
cur = next
return pre
```

### 思路 2:递归

具体做法如下:

Expand All @@ -52,24 +74,7 @@

![](https://qcdn.itcharge.cn/images/20220111134246.png)

## 代码

1. 迭代

```Python
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
pre = None
cur = head
while cur != None:
next = cur.next
cur.next = pre
pre = cur
cur = next
return pre
```

2. 递归
### 思路 2:递归代码

```Python
class Solution:
Expand Down

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