Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
leetcode
提交
5b576e3a
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 搜索 >>
提交
5b576e3a
编写于
3月 13, 2019
作者:
L
luzhipeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
双指针
上级
82abc55d
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
181 addition
and
0 deletion
+181
-0
19.remove-nth-node-from-end-of-list.js
19.remove-nth-node-from-end-of-list.js
+50
-0
assets/19.removeNthNodeFromEndOfList.gif
assets/19.removeNthNodeFromEndOfList.gif
+0
-0
removeNthNodeFromEndofList.md
removeNthNodeFromEndofList.md
+131
-0
未找到文件。
19.remove-nth-node-from-end-of-list.js
0 → 100644
浏览文件 @
5b576e3a
/*
* @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
)
{
};
assets/19.removeNthNodeFromEndOfList.gif
0 → 100644
浏览文件 @
5b576e3a
359.3 KB
removeNthNodeFromEndofList.md
0 → 100644
浏览文件 @
5b576e3a
## 题目地址
https://leetcode.com/problems/remove-nth-node-from-end-of-list/description
## 题目描述
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?
## 思路
双指针,指针A先移动n次, 指针B再开始移动。当A到达null的时候, 指针b的位置正好是倒数n
我们可以设想假设设定了双指针p和q的话,当q指向末尾的NULL,p与q之间相隔的元素个数为n时,那么删除掉p的下一个指针就完成了要求。
设置虚拟节点dummyHead指向head
设定双指针p和q,初始都指向虚拟节点dummyHead
移动q,直到p与q之间相隔的元素个数为n
同时移动p与q,直到q指向的为NULL
将p的下一个节点指向下下个节点
![
19.removeNthNodeFromEndOfList
](
./assets/19.removeNthNodeFromEndOfList.gif
)
(图片来自: https://github.com/MisterBooo/LeetCodeAnimation)
## 关键点解析
1.
链表这种数据结构的特点和使用
2.
使用双指针
3.
使用一个dummyHead简化操作
## 代码
```
js
/*
* @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
)
{
let
i
=
-
1
;
const
noop
=
{
next
:
null
};
const
dummyHead
=
new
ListNode
();
// 增加一个dummyHead 简化操作
dummyHead
.
next
=
head
;
let
currentP1
=
dummyHead
;
let
currentP2
=
dummyHead
;
while
(
currentP1
)
{
if
(
i
===
n
)
{
currentP2
=
currentP2
.
next
;
}
if
(
i
!==
n
)
{
i
++
;
}
currentP1
=
currentP1
.
next
;
}
currentP2
.
next
=
((
currentP2
||
noop
).
next
||
noop
).
next
;
return
dummyHead
.
next
;
};
```
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录