Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
leetcode
提交
469e1217
L
leetcode
项目概览
爱吃血肠
/
leetcode
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
L
leetcode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
469e1217
编写于
3月 15, 2019
作者:
L
luzhipeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
swap-nodes-in-pairs
上级
9d44a444
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
98 addition
and
50 deletion
+98
-50
19.remove-nth-node-from-end-of-list.js
19.remove-nth-node-from-end-of-list.js
+0
-50
README.md
README.md
+1
-0
assets/24.swap-nodes-in-pairs.gif
assets/24.swap-nodes-in-pairs.gif
+0
-0
swapNodesInPairs.md
swapNodesInPairs.md
+97
-0
未找到文件。
19.remove-nth-node-from-end-of-list.js
已删除
100644 → 0
浏览文件 @
9d44a444
/*
* @lc app=leetcode id=19 lang=javascript
*
* [19] Remove Nth Node From End of List
*
* https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
*
* algorithms
* Medium (34.03%)
* Total Accepted: 360.1K
* Total Submissions: 1.1M
* Testcase Example: '[1,2,3,4,5]\n2'
*
* Given a linked list, remove the n-th node from the end of list and return
* its head.
*
* Example:
*
*
* Given linked list: 1->2->3->4->5, and n = 2.
*
* After removing the second node from the end, the linked list becomes
* 1->2->3->5.
*
*
* Note:
*
* Given n will always be valid.
*
* Follow up:
*
* Could you do this in one pass?
*
*/
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} n
* @return {ListNode}
*/
var
removeNthFromEnd
=
function
(
head
,
n
)
{
};
README.md
浏览文件 @
469e1217
...
...
@@ -9,5 +9,6 @@ leetcode题解,记录自己的leecode解题之路。
-
[
3. Longest Substring Without Repeating Characters
](
https://github.com/azl397985856/leetcode/blob/master/longestSubstringWithoutRepeatingCharacters.md
)
-
[
5. Longest Palindromic Substring
](
https://github.com/azl397985856/leetcode/blob/master/longestPalindromicSubstring.md
)
-
[
19. Remove Nth Node From End of List
](
https://github.com/azl397985856/leetcode/blob/master/removeNthNodeFromEndofList.md
)
-
[
24. Swap Nodes In Pairs
](
https://github.com/azl397985856/leetcode/blob/master/swapNodesInPairs.md
)
### 高级难度
\ No newline at end of file
assets/24.swap-nodes-in-pairs.gif
0 → 100644
浏览文件 @
469e1217
332.8 KB
swapNodesInPairs.md
0 → 100644
浏览文件 @
469e1217
## 题目地址
https://leetcode.com/problems/swap-nodes-in-pairs/description/
## 题目描述
Given a linked list, swap every two adjacent nodes and return its head.
You may not modify the values in the list's nodes, only nodes itself may be changed.
Example:
Given 1->2->3->4, you should return the list as 2->1->4->3.
## 思路
设置一个dummy 节点简化操作。
设置一个虚拟头结点dummyHead
设置需要交换的两个节点分别为node1、node2,同时设置node2的下一个节点next
在这一轮操作中
将node2节点的next设置为node1节点
将node1节点的next设置为next节点
将dummyHead节点的next设置为node2
结束本轮操作
接下来的每轮操作都按照上述进行。
![
24.swap-nodes-in-pairs
](
./assets/24.swap-nodes-in-pairs.gif
)
(图片来自: https://github.com/MisterBooo/LeetCodeAnimation)
## 关键点解析
1.
链表这种数据结构的特点和使用
2.
dummyHead简化操作
## 代码
```
js
/*
* @lc app=leetcode id=24 lang=javascript
*
* [24] Swap Nodes in Pairs
*
* https://leetcode.com/problems/swap-nodes-in-pairs/description/
*
* algorithms
* Medium (43.33%)
* Total Accepted: 287.2K
* Total Submissions: 661.3K
* Testcase Example: '[1,2,3,4]'
*
* Given a linked list, swap every two adjacent nodes and return its head.
*
* You may not modify the values in the list's nodes, only nodes itself may be
* changed.
*
*
*
* Example:
*
*
* Given 1->2->3->4, you should return the list as 2->1->4->3.
*
*/
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var
swapPairs
=
function
(
head
)
{
const
dummy
=
new
ListNode
(
0
);
dummy
.
next
=
head
;
let
current
=
dummy
;
while
(
current
.
next
!=
null
&&
current
.
next
.
next
!=
null
)
{
const
first
=
current
.
next
;
const
second
=
current
.
next
.
next
;
first
.
next
=
second
.
next
;
current
.
next
=
second
;
current
.
next
.
next
=
first
;
current
=
current
.
next
.
next
;
}
return
dummy
.
next
;
};
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录