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

242 lines
6.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#### [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
low = temp
****
Java Code:
```java
class Solution {
public ListNode reverseList(ListNode head) {
//特殊情况
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;
}
}
```
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
class Solution {
public:
ListNode* reverseList(ListNode* head) {
//特殊情况
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;
}
};
```
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
```
****
Java Code:
```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;
}
}
```
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++:
```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;
}
};
```
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)
> ```