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

249 lines
7.0 KiB
Java
Raw Normal View History

2021-07-15 16:06:52 +00:00
> **[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>进入。
2021-03-23 12:08:09 +00:00
2021-07-15 16:06:52 +00:00
2021-03-23 12:08:09 +00:00
#### [206. ](https://leetcode-cn.com/problems/reverse-linked-list/)
**:**
> : 1->2->3->4->5->NULL
> : 5->4->3->2->1->NULL
AC
![](https://img-blog.csdnimg.cn/20210323191331552.gif)
low pro head
2021-07-15 16:06:52 +00:00
temp pro
2021-03-23 12:08:09 +00:00
2021-07-15 16:06:52 +00:00
pro pro = pro.next
2021-03-23 12:08:09 +00:00
temp low
2021-07-13 04:27:36 +00:00
low = temp
2021-03-23 12:08:09 +00:00
2021-07-13 04:27:36 +00:00
2021-03-23 12:08:09 +00:00
2021-04-28 10:28:00 +00:00
****
2021-04-27 10:12:18 +00:00
Java Code:
2021-03-23 12:08:09 +00:00
```java
class Solution {
public ListNode reverseList(ListNode head) {
2021-07-13 04:27:36 +00:00
//特殊情况
2021-03-23 12:08:09 +00:00
if (head == null || head.next == null) {
return head;
}
ListNode low = null;
ListNode pro = head;
while (pro != null) {
//代表橙色指针
ListNode temp = pro;
//移动绿色指针
pro = pro.next;
//反转节点
temp.next = low;
//移动黄色指针
low = temp;
}
return low;
}
}
```
2021-07-13 04:27:36 +00:00
C++ Code:
2021-04-28 10:28:00 +00:00
```cpp
class Solution {
public:
ListNode* reverseList(ListNode* head) {
2021-07-13 04:27:36 +00:00
//特殊情况
2021-04-28 10:28:00 +00:00
if (head == nullptr || head->next == nullptr) {
return head;
}
ListNode * low = nullptr;
ListNode * pro = head;
while (pro != nullptr) {
//代表橙色指针
ListNode * temp = pro;
//移动绿色指针
pro = pro->next;
//反转节点
temp->next = low;
//移动黄色指针
low = temp;
}
return low;
}
};
```
2021-07-15 16:06:52 +00:00
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;
};
```
2021-07-13 04:27:36 +00:00
Python Code:
2021-07-15 16:06:52 +00:00
```python
2021-07-13 04:27:36 +00:00
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
2021-07-15 16:06:52 +00:00
#
2021-07-13 04:27:36 +00:00
if head is None or head.next is None:
return head
low = None
pro = head
while pro is not None:
#
temp = pro
# 绿
pro = pro.next
#
temp.next = low
#
low = temp
return low
```
2021-03-23 12:08:09 +00:00
2021-04-28 10:28:00 +00:00
****
2021-04-27 10:12:18 +00:00
Java Code:
2021-03-23 12:08:09 +00:00
```java
class Solution {
public ListNode reverseList(ListNode head) {
//结束条件
if (head == null || head.next == null) {
return head;
}
//保存最后一个节点
ListNode pro = reverseList(head.next);
2021-07-15 16:06:52 +00:00
//将节点进行反转。我们可以这样理解 4.next.next = 4
//4.next = 5
2021-03-23 12:08:09 +00:00
//则 5.next = 4 则实现了反转
head.next.next = head;
//防止循环
head.next = null;
return pro;
}
}
```
2021-07-15 16:06:52 +00:00
C++ Code:
2021-04-28 10:28:00 +00:00
```cpp
class Solution {
public:
ListNode * reverseList(ListNode * head) {
//结束条件
if (head == nullptr || head->next == nullptr) {
return head;
}
//保存最后一个节点
ListNode * pro = reverseList(head->next);
2021-07-15 16:06:52 +00:00
//将节点进行反转。我们可以这样理解 4->next->next = 4
//4->next = 5
2021-04-28 10:28:00 +00:00
//则 5->next = 4 则实现了反转
head->next->next = head;
//防止循环
head->next = nullptr;
return pro;
}
};
```
2021-07-15 16:06:52 +00:00
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;
};
```
2021-07-13 04:27:36 +00:00
Python Code:
2021-07-15 16:06:52 +00:00
```python
2021-07-13 04:27:36 +00:00
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
#
if head is None or head.next is None:
return head
#
pro = self.reverseList(head.next)
2021-07-15 16:06:52 +00:00
# 4->next->next = 4
# 4->next = 5
2021-07-13 04:27:36 +00:00
# 5->next = 4
head.next.next = head
#
head.next = None
return pro
```
<br/>
> [@jaredliw](https://github.com/jaredliw)注:
>
2021-07-15 16:06:52 +00:00
>
2021-07-13 04:27:36 +00:00
>
2021-07-15 16:06:52 +00:00
> ```python
2021-07-13 04:27:36 +00:00
> class Solution:
2021-07-15 16:06:52 +00:00
> 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)
2021-07-13 04:27:36 +00:00
> ```