验证,校对

This commit is contained in:
jaredliw
2021-07-16 00:06:52 +08:00
parent 4a8c81e88e
commit 88fcd88712
14 changed files with 180 additions and 197 deletions

View File

@@ -1,6 +1,12 @@
> 如果阅读时发现错误或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
> 感谢支持该仓库会一直维护希望对各位有一丢丢帮助
>
> 另外希望手机阅读的同学可以来我的 <u>[**公众号袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
今天咱们说一道非常简单但是很经典的面试题思路很容易但是里面细节挺多所以我们还是需要注意
我们先来看一下题目描述
我们先来看一下题目描述
#### [206. 反转链表](https://leetcode-cn.com/problems/reverse-linked-list/)
@@ -19,9 +25,9 @@
原理很容易理解我们首先将 low 指针指向空节点 pro 节点指向 head 节点
然后我们定义一个临时节点指向 pro 节点
然后我们定义一个临时节点 temp 指向 pro 节点
此时我们就记住了 pro 节点的位置然后 pro = pro.next.这样我们三个指针指向三个不同的节点
此时我们就记住了 pro 节点的位置然后 pro = pro.next这样我们三个指针指向三个不同的节点
则我们将 temp 指针指向 low 节点此时则完成了反转
@@ -59,29 +65,6 @@ class Solution {
}
}
```
JS Code:
```javascript
var reverseList = function(head) {
//特殊情况
if(!head || !head.next) {
return head;
}
let low = null;
let pro = head;
while (pro) {
//代表橙色指针
let temp = pro;
//移动绿色指针
pro = pro.next;
//反转节点
temp.next = low;
//移动黄色指针
low = temp;
}
return low;
};
```
C++ Code:
```cpp
@@ -109,12 +92,36 @@ public:
};
```
JS Code:
```javascript
var reverseList = function(head) {
//特殊情况
if(!head || !head.next) {
return head;
}
let low = null;
let pro = head;
while (pro) {
//代表橙色指针
let temp = pro;
//移动绿色指针
pro = pro.next;
//反转节点
temp.next = low;
//移动黄色指针
low = temp;
}
return low;
};
```
Python Code:
```py
```python
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
//特殊情况
# 特殊情况
if head is None or head.next is None:
return head
low = None
@@ -147,8 +154,8 @@ class Solution {
}
//保存最后一个节点
ListNode pro = reverseList(head.next);
//将节点进行反转。我们可以这样理解 4.next.next = 4;
//4.next = 5
//将节点进行反转。我们可以这样理解 4.next.next = 4
//4.next = 5
//则 5.next = 4 则实现了反转
head.next.next = head;
//防止循环
@@ -156,29 +163,9 @@ class Solution {
return pro;
}
}
```
JS Code:
```javascript
var reverseList = function(head) {
//结束条件
if (!head || !head.next) {
return head;
}
//保存最后一个节点
let pro = reverseList(head.next);
//将节点进行反转。我们可以这样理解 4.next.next = 4;
//4.next = 5
//则 5.next = 4 则实现了反转
head.next.next = head;
//防止循环
head.next = null;
return pro;
};
```
C++代码:
C++ Code:
```cpp
class Solution {
@@ -190,8 +177,8 @@ public:
}
//保存最后一个节点
ListNode * pro = reverseList(head->next);
//将节点进行反转。我们可以这样理解 4->next->next = 4;
//4->next = 5
//将节点进行反转。我们可以这样理解 4->next->next = 4
//4->next = 5
//则 5->next = 4 则实现了反转
head->next->next = head;
//防止循环
@@ -201,9 +188,29 @@ public:
};
```
JS Code:
```javascript
var reverseList = function(head) {
//结束条件
if (!head || !head.next) {
return head;
}
//保存最后一个节点
let pro = reverseList(head.next);
//将节点进行反转。我们可以这样理解 4.next.next = 4
//4.next = 5
//则 5.next = 4 则实现了反转
head.next.next = head;
//防止循环
head.next = null;
return pro;
};
```
Python Code:
```py
```python
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
# 结束条件
@@ -211,8 +218,8 @@ class Solution:
return head
# 保存最后一个节点
pro = self.reverseList(head.next)
# 将节点进行反转我们可以这样理解 4->next->next = 4;
# 4->next = 5
# 将节点进行反转我们可以这样理解 4->next->next = 4
# 4->next = 5
# 5->next = 4 则实现了反转
head.next.next = head
# 防止循环
@@ -224,18 +231,18 @@ class Solution:
> 贡献者[@jaredliw](https://github.com/jaredliw)注:
>
> 这里提供一个比较直观的递归写法供大家参考
> 这里提供一个比较直观的递归写法供大家参考由于代码比较直白其它语言的我就不写啦
>
> ```py
> ```python
> class Solution:
> def reverseList(self, head: ListNode, prev_nd: ListNode = None) -> ListNode:
> # 结束条件
> if head is None:
> return prev_nd
> # 记录下一个节点并反转
> next_nd = head.next
> head.next = prev_nd
> # 给定下一组该反转的节点
> return self.reverseList(next_nd, head)
> def reverseList(self, head: ListNode, prev_nd: ListNode = None) -> ListNode:
> # 结束条件
> if head is None:
> return prev_nd
> # 记录下一个节点并反转
> next_nd = head.next
> head.next = prev_nd
> # 给定下一组该反转的节点
> return self.reverseList(next_nd, head)
> ```