Skip to content

Commit dc3b469

Browse files
committed
add solution for leetcode 19
1 parent ae52aec commit dc3b469

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ LeetCode
393393
|22|[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/)| [js](./algorithms/generateParentheses/generateParentheses.js) |Medium|
394394
|21|[Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/)| |Easy|
395395
|20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/)| |Easy|
396-
|19|[Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/)| |Easy|
396+
|19|[Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/)| [js](./algorithms/removeNthNodeFromEndOfList/removeNthNodeFromEndOfList.js) |Easy|
397397
|18|[4Sum](https://leetcode.com/problems/4sum/)| |Medium|
398398
|17|[Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)| |Medium|
399399
|16|[3Sum Closest](https://leetcode.com/problems/3sum-closest/)| |Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
let removeNthFromEnd = function (head, n) {
2+
// 添加哨兵节点
3+
let dummy = new ListNode(0);
4+
dummy.next = head;
5+
let fast = dummy, slow = dummy;
6+
// 快指针先走 n+1 步
7+
while(n--) {
8+
fast = fast.next;
9+
}
10+
// 快慢指针同时走
11+
// 快指针走到最后的时候,慢指针的位置就是第N个节点的前一个节点的位置
12+
while(fast && fast.next) {
13+
fast = fast.next;
14+
slow = slow.next;
15+
}
16+
// 删除这个节点
17+
slow.next = slow.next.next;
18+
return dummy.next;
19+
}

0 commit comments

Comments
 (0)