LeetCode每日一練【25】
阿新 • • 發佈:2022-05-07
# 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) } ```