LeetCode每日一练【25】
作者:互联网
# Reverse Nodes in K Group
## 我的提交
### 介绍
阿西吧!
### 思路
递归算法 + 闭包
1. 将链表等分为n个分组 2. 翻转单个分组的指向 3. 连接所有分组
### 代码
```js /* * @Author: fox * @Date: 2022-05-06 10:25:12 * @LastEditors: fox * @LastEditTime: 2022-05-06 17:21:10 * @Description: https://leetcode.com/problems/reverse-nodes-in-k-group/ */
class ListNode { constructor (val, next) { this.val = (val === undefined ? 0 : val); this.next = (next === undefined ? null : next); } }
/** * @description: Runtime: 59.10% Memory Usage: 74.54% * @param {*} head * @param {*} k * @return {*} */ const reverseKGroup = (head, k) => { /** * @description: 递归算法,将reverse中得到的翻转分组的新起始位置的节点指针挂载到前一个分组的末尾 * @param {*} start * @return {*} */ const reverseAGroup = (start) => { let temp_k = k; let curr = start; while(curr && temp_k-- > 0) { curr = curr.next; } if(temp_k > 0) { return start; } const groupTail = start; const groupHead = reverse(start, curr); if(curr) { groupTail.next = reverseAGroup(curr); } return groupHead; }
/** * @description: 翻转一个分组的链表 * @param {*} currGroupPointer 当前分组的起始位置的指针 * @param {*} nextGroupPointer 下一个分组的起始位置的指针 * @return {*} */ const reverse = (currGroupPointer, nextGroupPointer) => { let currPointer = currGroupPointer; // 当前分组的起始位置指针 let prev = null; // currPointer指针的前一个节点指针 let next = null; // currPointer指针的后一个节点指针
while (currPointer && currPointer !== nextGroupPointer) { next = currPointer.next; // 指向currPointer的后一个节点 currPointer.next = prev; // currentPointer.next指向currPointer的前一个节点 prev = currPointer; // 将前一个节点指向当前节点currPointer currPointer = next; // 移动当前节点到下一个节点 }
return prev; // 返回翻转分组的起始位置的节点 }
return reverseAGroup(head) } ```
## 我的提交
### 介绍
阿西吧!
### 思路
递归算法 + 闭包
1. 将链表等分为n个分组 2. 翻转单个分组的指向 3. 连接所有分组
### 代码
```js /* * @Author: fox * @Date: 2022-05-06 10:25:12 * @LastEditors: fox * @LastEditTime: 2022-05-06 17:21:10 * @Description: https://leetcode.com/problems/reverse-nodes-in-k-group/ */
class ListNode { constructor (val, next) { this.val = (val === undefined ? 0 : val); this.next = (next === undefined ? null : next); } }
/** * @description: Runtime: 59.10% Memory Usage: 74.54% * @param {*} head * @param {*} k * @return {*} */ const reverseKGroup = (head, k) => { /** * @description: 递归算法,将reverse中得到的翻转分组的新起始位置的节点指针挂载到前一个分组的末尾 * @param {*} start * @return {*} */ const reverseAGroup = (start) => { let temp_k = k; let curr = start; while(curr && temp_k-- > 0) { curr = curr.next; } if(temp_k > 0) { return start; } const groupTail = start; const groupHead = reverse(start, curr); if(curr) { groupTail.next = reverseAGroup(curr); } return groupHead; }
/** * @description: 翻转一个分组的链表 * @param {*} currGroupPointer 当前分组的起始位置的指针 * @param {*} nextGroupPointer 下一个分组的起始位置的指针 * @return {*} */ const reverse = (currGroupPointer, nextGroupPointer) => { let currPointer = currGroupPointer; // 当前分组的起始位置指针 let prev = null; // currPointer指针的前一个节点指针 let next = null; // currPointer指针的后一个节点指针
while (currPointer && currPointer !== nextGroupPointer) { next = currPointer.next; // 指向currPointer的后一个节点 currPointer.next = prev; // currentPointer.next指向currPointer的前一个节点 prev = currPointer; // 将前一个节点指向当前节点currPointer currPointer = next; // 移动当前节点到下一个节点 }
return prev; // 返回翻转分组的起始位置的节点 }
return reverseAGroup(head) } ```
标签:25,curr,每日,return,next,currPointer,分组,节点,LeetCode 来源: https://www.cnblogs.com/mapodoufu/p/16240999.html