-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add "swap-nodes-in-pairs" content
- Loading branch information
Showing
3 changed files
with
134 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
--- | ||
layout: doc | ||
--- | ||
|
||
# 24-两两交换链表中的节点 | ||
|
||
标签:<Badge type="tip" text="递归" /> <Badge type="tip" text="链表" /> | ||
|
||
## 题目信息 | ||
|
||
**题目地址**: [24. 两两交换链表中的节点](https://leetcode.cn/problems/swap-nodes-in-pairs/description/) | ||
|
||
**题目内容:** | ||
|
||
```javascript | ||
给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。 | ||
|
||
示例 1: | ||
输入:head = [1,2,3,4] | ||
输出:[2,1,4,3] | ||
|
||
示例 2: | ||
输入:head = [] | ||
输出:[] | ||
|
||
示例 3: | ||
输入:head = [1] | ||
输出:[1] | ||
|
||
提示: | ||
链表中节点的数目在范围 [0, 100] 内 | ||
0 <= Node.val <= 100 | ||
``` | ||
|
||
> 示例1图片如下: | ||
![swap_ex1](https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg) | ||
|
||
## 题解 | ||
|
||
### 解法一 | ||
|
||
**思路:** | ||
|
||
> 设置虚拟头节点`dummyHead`,它的`next`指向`head`,遍历链表,设置遍历节点`curNode`,默认指向`dummyHead` | ||
> | ||
> 当`curNode.next`和`curNode.next.next`都存在时,交换两个的位置 | ||
> | ||
> 直到`curNode.next`或者`curNode.next.next`为`null`时,结束循环 | ||
> 图解如下,看图会好理解一点 | ||
![24-双指针思路](https://raw.githubusercontent.com/mx52jing/image-hosting/main/images/algorithm-related/leetcode-24-swap-nodes-in-pairs.png) | ||
|
||
**代码实现:** | ||
|
||
```javascript | ||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
/** | ||
* @param {ListNode} head | ||
* @return {ListNode} | ||
*/ | ||
var swapPairs = function(head) { | ||
// 使用虚拟头节点 | ||
const dummyHead = new ListNode(0, head) | ||
let curNode = dummyHead | ||
while(curNode.next !== null && curNode.next.next !== null) { | ||
// 暂时保存curNode.next节点 为了交换时使用 | ||
const temp1 = curNode.next | ||
// 保存要交换的第二个节点的next节点 比如节点2的next/节点4的next | ||
const temp2 = curNode.next.next.next | ||
curNode.next = curNode.next.next | ||
curNode.next.next = temp1 | ||
temp1.next = temp2 | ||
// curNode.next.next.next = temp2 和上面的写法效果一致 | ||
curNode = curNode.next.next | ||
} | ||
return dummyHead.next | ||
}; | ||
``` | ||
|
||
### 解法二 | ||
|
||
**思路:** | ||
|
||
> 使用`递归`,理解了方法一后,可以轻松写出递归版本的代码 | ||
**代码实现:** | ||
|
||
```javascript | ||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
/** | ||
* @param {ListNode} head | ||
* @return {ListNode} | ||
*/ | ||
var swapPairs = function(head) { | ||
// 使用虚拟头节点 | ||
const dummyHead = new ListNode(0, head) | ||
let curNode = dummyHead | ||
const swapNodesInPairs = (curNode) => { | ||
// 终止递归 | ||
if(curNode.next === null || curNode.next.next === null) return | ||
const temp1 = curNode.next | ||
const temp2 = curNode.next.next.next | ||
curNode.next = curNode.next.next | ||
curNode.next.next = temp1 | ||
temp1.next = temp2 | ||
return swapNodesInPairs(curNode.next.next) | ||
} | ||
swapNodesInPairs(curNode) | ||
return dummyHead.next | ||
}; | ||
``` |