Skip to content

Commit

Permalink
feat: add "swap-nodes-in-pairs" content
Browse files Browse the repository at this point in the history
  • Loading branch information
mx52jing committed Apr 30, 2024
1 parent ef10da7 commit 44edfec
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ export default {
{ text: '203-移除链表元素', link: '/algorithm/LeetCode刷题/203-移除链表元素' },
{ text: '707-设计链表', link: '/algorithm/LeetCode刷题/707-设计链表' },
{ text: '206-反转链表', link: '/algorithm/LeetCode刷题/206-反转链表' },
{ text: '24-两两交换链表中的节点', link: '/algorithm/LeetCode刷题/24-两两交换链表中的节点' },
]
}
],
Expand Down
14 changes: 8 additions & 6 deletions docs/algorithm/LeetCode刷题/206-反转链表.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ layout: doc
```
> 示例 1:
![](https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg)
![rev1ex1](https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg)

> 输入:head = [1,2,3,4,5]
>
> 输出:[5,4,3,2,1]
> 示例 2:
![](https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg)
![rev1ex2](https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg)

> 输入:head = [1,2]
>
Expand Down Expand Up @@ -58,13 +58,15 @@ layout: doc
>
> 赋值的顺序为:
>
> 1、先声明`temp = cur.next`
> 1、先声明`temp = cur.next`,保存临时变量,也就是`cur.next`初始指向的节点
>
> 2、将`cur.next`指向`prev`
> 2、将`cur.next`指向`prev`,反转`cur.next`的指向
>
> 3、`prev`赋值为`cur`(`prev = cur)`
> 3、`prev`赋值为`cur`(`prev = cur)`,将`prev`右移一位
>
> 4、将`cur`赋值为`temp`
> 4、将`cur`赋值为`temp`,将`cur`右移一位
>
> 5、当`cur``null`时,遍历结束
>
> 最终的反转结果就是`绿色箭头`所展现的,图示如下:
Expand Down
125 changes: 125 additions & 0 deletions docs/algorithm/LeetCode刷题/24-两两交换链表中的节点.md
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
};
```

0 comments on commit 44edfec

Please sign in to comment.