File tree 2 files changed +41
-0
lines changed 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 21
21
13|[ Roman to Integer] ( ./0013-roman-to-integer.js ) |Easy|
22
22
14|[ Longest Common Prefix] ( ./0014-longest-common-prefix.js ) |Easy|
23
23
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|
24
25
20|[ Valid Parentheses] ( ./0020-valid-parentheses.js ) |Easy|
25
26
21|[ Merge Two Sorted Lists] ( ./0021-merge-two-sorted-lists.js ) |Easy|
26
27
27|[ Remove Element] ( ./0027-remove-element.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments