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

267 lines
8.2 KiB
Java
Raw Normal View History

2021-03-26 11:58:47 +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-07-15 16:06:52 +00:00
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)。
2021-03-26 11:58:47 +00:00
#### [92. II](https://leetcode-cn.com/problems/reverse-linked-list-ii/)
`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
2021-03-27 08:38:40 +00:00
![](https://img-blog.csdnimg.cn/20210327163804112.gif)
2021-03-26 11:58:47 +00:00
2021-04-28 10:28:00 +00:00
****
Java Code:
2021-03-26 11:58:47 +00:00
```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;
}
2021-07-13 15:39:22 +00:00
//保存 left 节点前的一个节点
2021-03-26 11:58:47 +00:00
ListNode leftNode = pro;
2021-07-13 15:39:22 +00:00
//来到 right 节点
2021-03-26 11:58:47 +00:00
for (; i < right; ++i) {
pro = pro.next;
}
2021-07-13 15:39:22 +00:00
//保存 right 节点后的一个节点
2021-03-26 11:58:47 +00:00
ListNode rightNode = pro.next;
//切断链表
2021-07-13 15:39:22 +00:00
pro.next = null;//切断 right 后的部分
ListNode newhead = leftNode.next;//保存 left 节点
leftNode.next = null;//切断 left 前的部分
//反转
leftNode.next = reverse(newhead);
2021-03-26 11:58:47 +00:00
//重新接头
newhead.next = rightNode;
return temp.next;
}
//和反转链表1代码一致
2021-07-13 15:39:22 +00:00
public ListNode reverse (ListNode head) {
ListNode low = null;
2021-03-26 11:58:47 +00:00
ListNode pro = head;
while (pro != null) {
ListNode temp = pro;
pro = pro.next;
temp.next = low;
low = temp;
}
return low;
}
}
```
2021-04-28 10:28:00 +00:00
C++ Code:
```cpp
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int left, int right) {
2021-07-13 15:39:22 +00:00
//虚拟头节点
2021-04-28 10:28:00 +00:00
ListNode * temp = new ListNode(-1);
temp->next = head;
ListNode * pro = temp;
//来到 left 节点前的一个节点
int i = 0;
for (; i < left-1; ++i) {
pro = pro->next;
}
2021-07-13 15:39:22 +00:00
//保存 left 节点前的一个节点
2021-04-28 10:28:00 +00:00
ListNode * leftNode = pro;
2021-07-13 15:39:22 +00:00
//来到 right 节点
2021-04-28 10:28:00 +00:00
for (; i < right; ++i) {
pro = pro->next;
}
2021-07-13 15:39:22 +00:00
//保存 right 节点后的一个节点
2021-04-28 10:28:00 +00:00
ListNode * rightNode = pro->next;
//切断链表
2021-07-13 15:39:22 +00:00
pro->next = nullptr;//切断 right 后的部分
ListNode * newhead = leftNode->next;//保存 left 节点
leftNode->next = nullptr;//切断 left 前的部分
//反转
leftNode->next = reverse(newhead);
2021-04-28 10:28:00 +00:00
//重新接头
newhead->next = rightNode;
return temp->next;
}
2021-07-13 15:39:22 +00:00
//和反转链表1代码一致
ListNode * reverse (ListNode * head) {
2021-04-28 10:28:00 +00:00
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 15:39:22 +00:00
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:
2021-07-15 16:06:52 +00:00
```python
2021-07-13 15:39:22 +00:00
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
```
2021-07-17 14:28:06 +00:00
Swift Code
```swift
class Solution {
func reverseBetween(_ head: ListNode?, _ left: Int, _ right: Int) -> ListNode? {
// 虚拟头结点
var temp = ListNode(-1)
temp.next = head
var pro:ListNode? = temp
// 来到 left 节点前的一个节点
var i = 0
for n in i..<left - 1 {
pro = pro?.next
i += 1
}
// 保存 left 节点前的一个节点
var leftNode = pro
// 来到 right 节点
for n in i..<right {
pro = pro?.next
}
// 保存 right 节点后的一个节点
var rightNode:ListNode? = pro?.next
// 切断链表
pro?.next = nil // 切断 right 后的部分
var newHead:ListNode? = leftNode?.next // 保存 left 节点
leftNode?.next = nil // 切断 left 前的部分
// 反转
leftNode?.next = reverse(newHead)
// 重新接头
newHead?.next = rightNode
return temp.next
}
// 和反转链表1代码一致
func reverse(_ head: ListNode?) -> ListNode? {
var low:ListNode?
var pro = head
while pro != nil {
var temp = pro
pro = pro?.next
temp?.next = low
low = temp
}
return low
}
}
```