mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2024-11-24 13:03:41 +00:00
添加py和js,添加注释
This commit is contained in:
parent
2e9819611c
commit
474937e864
@ -43,7 +43,6 @@ Java Code:
|
|||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
public ListNode reverseBetween(ListNode head, int left, int right) {
|
public ListNode reverseBetween(ListNode head, int left, int right) {
|
||||||
|
|
||||||
//虚拟头节点
|
//虚拟头节点
|
||||||
ListNode temp = new ListNode(-1);
|
ListNode temp = new ListNode(-1);
|
||||||
temp.next = head;
|
temp.next = head;
|
||||||
@ -53,26 +52,28 @@ class Solution {
|
|||||||
for (; i < left-1; ++i) {
|
for (; i < left-1; ++i) {
|
||||||
pro = pro.next;
|
pro = pro.next;
|
||||||
}
|
}
|
||||||
// 保存 left 节点前的第一个节点
|
//保存 left 节点前的一个节点
|
||||||
ListNode leftNode = pro;
|
ListNode leftNode = pro;
|
||||||
|
//来到 right 节点
|
||||||
for (; i < right; ++i) {
|
for (; i < right; ++i) {
|
||||||
pro = pro.next;
|
pro = pro.next;
|
||||||
}
|
}
|
||||||
// 保存 right 节点后的节点
|
//保存 right 节点后的一个节点
|
||||||
ListNode rightNode = pro.next;
|
ListNode rightNode = pro.next;
|
||||||
//切断链表
|
//切断链表
|
||||||
pro.next = null;
|
pro.next = null;//切断 right 后的部分
|
||||||
ListNode newhead = leftNode.next;
|
ListNode newhead = leftNode.next;//保存 left 节点
|
||||||
leftNode.next = null;
|
leftNode.next = null;//切断 left 前的部分
|
||||||
leftNode.next = rever(newhead);
|
//反转
|
||||||
|
leftNode.next = reverse(newhead);
|
||||||
//重新接头
|
//重新接头
|
||||||
newhead.next = rightNode;
|
newhead.next = rightNode;
|
||||||
return temp.next;
|
return temp.next;
|
||||||
|
|
||||||
}
|
}
|
||||||
//和反转链表1代码一致
|
//和反转链表1代码一致
|
||||||
public ListNode rever (ListNode head) {
|
public ListNode reverse (ListNode head) {
|
||||||
ListNode low = null;
|
ListNode low = null;
|
||||||
ListNode pro = head;
|
ListNode pro = head;
|
||||||
while (pro != null) {
|
while (pro != null) {
|
||||||
ListNode temp = pro;
|
ListNode temp = pro;
|
||||||
@ -91,6 +92,7 @@ C++ Code:
|
|||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
ListNode* reverseBetween(ListNode* head, int left, int right) {
|
ListNode* reverseBetween(ListNode* head, int left, int right) {
|
||||||
|
//虚拟头节点
|
||||||
ListNode * temp = new ListNode(-1);
|
ListNode * temp = new ListNode(-1);
|
||||||
temp->next = head;
|
temp->next = head;
|
||||||
ListNode * pro = temp;
|
ListNode * pro = temp;
|
||||||
@ -99,23 +101,26 @@ public:
|
|||||||
for (; i < left-1; ++i) {
|
for (; i < left-1; ++i) {
|
||||||
pro = pro->next;
|
pro = pro->next;
|
||||||
}
|
}
|
||||||
// 保存 left 节点前的第一个节点
|
//保存 left 节点前的一个节点
|
||||||
ListNode * leftNode = pro;
|
ListNode * leftNode = pro;
|
||||||
|
//来到 right 节点
|
||||||
for (; i < right; ++i) {
|
for (; i < right; ++i) {
|
||||||
pro = pro->next;
|
pro = pro->next;
|
||||||
}
|
}
|
||||||
// 保存 right 节点后的节点
|
//保存 right 节点后的一个节点
|
||||||
ListNode * rightNode = pro->next;
|
ListNode * rightNode = pro->next;
|
||||||
//切断链表
|
//切断链表
|
||||||
pro->next = nullptr;
|
pro->next = nullptr;//切断 right 后的部分
|
||||||
ListNode * newhead = leftNode->next;
|
ListNode * newhead = leftNode->next;//保存 left 节点
|
||||||
leftNode->next = nullptr;
|
leftNode->next = nullptr;//切断 left 前的部分
|
||||||
leftNode->next = rever(newhead);
|
//反转
|
||||||
|
leftNode->next = reverse(newhead);
|
||||||
//重新接头
|
//重新接头
|
||||||
newhead->next = rightNode;
|
newhead->next = rightNode;
|
||||||
return temp->next;
|
return temp->next;
|
||||||
}
|
}
|
||||||
ListNode * rever (ListNode * head) {
|
//和反转链表1代码一致
|
||||||
|
ListNode * reverse (ListNode * head) {
|
||||||
ListNode * low = nullptr;
|
ListNode * low = nullptr;
|
||||||
ListNode * pro = head;
|
ListNode * pro = head;
|
||||||
while (pro != nullptr) {
|
while (pro != nullptr) {
|
||||||
@ -129,3 +134,90 @@ public:
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
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
|
||||||
|
```
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user