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

242 lines
6.1 KiB
Java
Raw Normal View History

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
pro
pro pro = pro.next.
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-04-27 10:12:18 +00:00
JS Code:
```javascript
var reverseList = function(head) {
2021-07-13 04:27:36 +00:00
//特殊情况
2021-04-27 10:12:18 +00:00
if(!head || !head.next) {
return head;
}
let low = null;
let pro = head;
while (pro) {
2021-07-13 04:27:36 +00:00
//代表橙色指针
2021-04-27 10:12:18 +00:00
let temp = pro;
2021-07-13 04:27:36 +00:00
//移动绿色指针
2021-04-27 10:12:18 +00:00
pro = pro.next;
2021-07-13 04:27:36 +00:00
//反转节点
2021-04-27 10:12:18 +00:00
temp.next = low;
2021-07-13 04:27:36 +00:00
//移动黄色指针
2021-04-27 10:12:18 +00:00
low = temp;
}
return low;
};
```
2021-03-23 12:08:09 +00:00
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-13 04:27:36 +00:00
Python Code:
```py
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
//特殊情况
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);
//将节点进行反转。我们可以这样理解 4.next.next = 4;
//4.next = 5
//则 5.next = 4 则实现了反转
head.next.next = head;
//防止循环
head.next = null;
return pro;
}
}
```
2021-04-27 10:12:18 +00:00
JS Code:
```javascript
var reverseList = function(head) {
2021-07-13 04:27:36 +00:00
//结束条件
2021-04-27 10:12:18 +00:00
if (!head || !head.next) {
return head;
}
2021-07-13 04:27:36 +00:00
//保存最后一个节点
2021-04-27 10:12:18 +00:00
let pro = reverseList(head.next);
2021-07-13 04:27:36 +00:00
//将节点进行反转。我们可以这样理解 4.next.next = 4;
//4.next = 5
//则 5.next = 4 则实现了反转
2021-04-27 10:12:18 +00:00
head.next.next = head;
2021-07-13 04:27:36 +00:00
//防止循环
2021-04-27 10:12:18 +00:00
head.next = null;
return pro;
};
```
2021-04-28 10:28:00 +00:00
C++:
```cpp
class Solution {
public:
ListNode * reverseList(ListNode * head) {
//结束条件
if (head == nullptr || head->next == nullptr) {
return head;
}
//保存最后一个节点
ListNode * pro = reverseList(head->next);
//将节点进行反转。我们可以这样理解 4->next->next = 4;
//4->next = 5
//则 5->next = 4 则实现了反转
head->next->next = head;
//防止循环
head->next = nullptr;
return pro;
}
};
```
2021-07-13 04:27:36 +00:00
Python Code:
```py
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
#
if head is None or head.next is None:
return head
#
pro = self.reverseList(head.next)
# 4->next->next = 4;
# 4->next = 5
# 5->next = 4
head.next.next = head
#
head.next = None
return pro
```
<br/>
> [@jaredliw](https://github.com/jaredliw)注:
>
>
>
> ```py
> 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)
> ```