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

224 lines
6.9 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.

> **[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>进入。
2 1 1 [leetcode 206 ](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/leetcode206%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A8.md)
#### [92. II](https://leetcode-cn.com/problems/reverse-linked-list-ii/)
836
`head` `left` `right` `left <= right` `left` `right` ****
** 1**
![img](https://assets.leetcode.com/uploads/2021/02/19/rev2ex2.jpg)
> head = [1,2,3,4,5], left = 2, right = 4
> [1,4,3,2,5]
** 2**
> head = [5], left = 1, right = 1
> [5]
1
![](https://img-blog.csdnimg.cn/20210327163804112.gif)
****
Java Code:
```java
class Solution {
public ListNode reverseBetween(ListNode head, int left, int right) {
//虚拟头节点
ListNode temp = new ListNode(-1);
temp.next = head;
ListNode pro = temp;
//来到 left 节点前的一个节点
int i = 0;
for (; i < left-1; ++i) {
pro = pro.next;
}
//保存 left 节点前的一个节点
ListNode leftNode = pro;
//来到 right 节点
for (; i < right; ++i) {
pro = pro.next;
}
//保存 right 节点后的一个节点
ListNode rightNode = pro.next;
//切断链表
pro.next = null;//切断 right 后的部分
ListNode newhead = leftNode.next;//保存 left 节点
leftNode.next = null;//切断 left 前的部分
//反转
leftNode.next = reverse(newhead);
//重新接头
newhead.next = rightNode;
return temp.next;
}
//和反转链表1代码一致
public ListNode reverse (ListNode head) {
ListNode low = null;
ListNode pro = head;
while (pro != null) {
ListNode temp = pro;
pro = pro.next;
temp.next = low;
low = temp;
}
return low;
}
}
```
C++ Code:
```cpp
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int left, int right) {
//虚拟头节点
ListNode * temp = new ListNode(-1);
temp->next = head;
ListNode * pro = temp;
//来到 left 节点前的一个节点
int i = 0;
for (; i < left-1; ++i) {
pro = pro->next;
}
//保存 left 节点前的一个节点
ListNode * leftNode = pro;
//来到 right 节点
for (; i < right; ++i) {
pro = pro->next;
}
//保存 right 节点后的一个节点
ListNode * rightNode = pro->next;
//切断链表
pro->next = nullptr;//切断 right 后的部分
ListNode * newhead = leftNode->next;//保存 left 节点
leftNode->next = nullptr;//切断 left 前的部分
//反转
leftNode->next = reverse(newhead);
//重新接头
newhead->next = rightNode;
return temp->next;
}
//和反转链表1代码一致
ListNode * reverse (ListNode * head) {
ListNode * low = nullptr;
ListNode * pro = head;
while (pro != nullptr) {
ListNode * temp = pro;
pro = pro->next;
temp->next = low;
low = temp;
}
return low;
}
};
```
JS Code:
```js
var reverseBetween = function(head, left, right) {
//虚拟头节点
let temp = new ListNode(-1);
temp.next = head;
let pro = temp;
//来到 left 节点前的一个节点
let i = 0;
for (; i < left-1; ++i) {
pro = pro.next;
}
//保存 left 节点前的一个节点
let leftNode = pro;
//来到 right 节点
for (; i < right; ++i) {
pro = pro.next;
}
//保存 right 节点后的一个节点
let rightNode = pro.next;
//切断链表
pro.next = null;//切断 right 后的部分
let newhead = leftNode.next;//保存 left 节点
leftNode.next = null;//切断 left 前的部分
//反转
leftNode.next = reverse(newhead);
//重新接头
newhead.next = rightNode;
return temp.next;
};
//和反转链表1代码一致
var reverse = function(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
class Solution:
def reverseBetween(self, head: ListNode, left: int, right: int) -> ListNode:
#
temp = ListNode(-1)
temp.next = head
pro = temp
# left
for _ in range(left - 1):
pro = pro.next
# left
leftNode = pro
for _ in range(right - left + 1):
pro = pro.next
# right
rightNode = pro.next
#
pro.next = None # right
newhead = leftNode.next # left
leftNode.next = None # left
#
leftNode.next = self.reverse(newhead)
#
newhead.next = rightNode
return temp.next
# 1
def reverse(self, head):
low = None
pro = head
while pro is not None:
temp = pro
pro = pro.next
temp.next = low
low = temp
return low
```