Skip to content

Commit 32ff5b3

Browse files
committed
Add solution #19
1 parent 7bcfb4d commit 32ff5b3

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
13|[Roman to Integer](./0013-roman-to-integer.js)|Easy|
2222
14|[Longest Common Prefix](./0014-longest-common-prefix.js)|Easy|
2323
17|[Letter Combinations of a Phone Number](./0017-letter-combinations-of-a-phone-number.js)|Medium|
24+
19|[Remove Nth Node From End of List](./0019-remove-nth-node-from-end-of-list.js)|Medium|
2425
20|[Valid Parentheses](./0020-valid-parentheses.js)|Easy|
2526
21|[Merge Two Sorted Lists](./0021-merge-two-sorted-lists.js)|Easy|
2627
27|[Remove Element](./0027-remove-element.js)|Easy|
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* 19. Remove Nth Node From End of List
3+
* https://leetcode.com/problems/remove-nth-node-from-end-of-list/
4+
* Difficulty: Medium
5+
*
6+
* Given the head of a linked list, remove the nth node from the end of the list
7+
* and return its head.
8+
*/
9+
10+
/**
11+
* Definition for singly-linked list.
12+
* function ListNode(val, next) {
13+
* this.val = (val===undefined ? 0 : val)
14+
* this.next = (next===undefined ? null : next)
15+
* }
16+
*/
17+
/**
18+
* @param {ListNode} head
19+
* @param {number} n
20+
* @return {ListNode}
21+
*/
22+
var removeNthFromEnd = function(head, n) {
23+
const result = new ListNode();
24+
let slow = result;
25+
let fast = result;
26+
slow.next = head;
27+
28+
for (let i = 0; i <= n; i++) {
29+
fast = fast.next;
30+
}
31+
32+
while (fast) {
33+
fast = fast.next;
34+
slow = slow.next;
35+
}
36+
37+
slow.next = slow.next.next;
38+
39+
return result.next;
40+
};

0 commit comments

Comments
 (0)