File tree 2 files changed +89
-0
lines changed
2 files changed +89
-0
lines changed Original file line number Diff line number Diff line change 19
19
| 9| [ 回文数] ( https://leetcode-cn.com/problems/palindrome-number/ ) | [ JavaScript] ( ./algorithms/palindrome-number.js ) | Easy|
20
20
| 13| [ 罗马数字转整数] ( https://leetcode.cn/problems/roman-to-integer/ ) | [ JavaScript] ( ./algorithms/roman-to-integer.js ) | Easy|
21
21
| 14| [ 最长公共前缀] ( https://leetcode-cn.com/problems/longest-common-prefix/ ) | [ JavaScript] ( ./algorithms/longest-common-prefix.js ) | Easy|
22
+ | 19| [ 删除链表的倒数第 N 个结点] ( https://leetcode.cn/problems/remove-nth-node-from-end-of-list/ ) | [ JavaScript] ( ./algorithms/remove-nth-node-from-end-of-list.js ) | Medium|
22
23
| 20| [ 有效的括号] ( https://leetcode.cn/problems/valid-parentheses/ ) | [ JavaScript] ( ./algorithms/valid-parentheses.js ) | Easy|
23
24
| 21| [ 合并两个有序链表] ( https://leetcode-cn.com/problems/merge-two-sorted-lists/ ) | [ JavaScript] ( ./algorithms/merge-two-sorted-lists.js ) | Easy|
24
25
| 24| [ 两两交换链表中的节点] ( https://leetcode.cn/problems/swap-nodes-in-pairs/ ) | [ JavaScript] ( ./algorithms/swap-nodes-in-pairs.js ) | Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * function ListNode(val, next) {
4
+ * this.val = (val===undefined ? 0 : val)
5
+ * this.next = (next===undefined ? null : next)
6
+ * }
7
+ */
8
+ /**
9
+ * @param {ListNode } head
10
+ * @param {number } n
11
+ * @return {ListNode }
12
+ */
13
+ var removeNthFromEnd = function ( head , n ) {
14
+ // 常规做法 计算链表长度
15
+ let dummyNode = new ListNode ( - 1 ) ;
16
+ dummyNode . next = head ;
17
+ let length = getLength ( head ) ;
18
+ let curr = dummyNode ;
19
+
20
+ for ( let i = 1 ; i < length - n + 1 ; i ++ ) {
21
+ curr = curr . next ;
22
+ }
23
+ curr . next = curr . next . next ;
24
+
25
+ return dummyNode . next ;
26
+
27
+ // 栈
28
+ // return stackFn(head);
29
+
30
+ // 双指针
31
+ // return twoPointers(head);
32
+ } ;
33
+
34
+ // 计算链表长度
35
+ function getLength ( head ) {
36
+ let curr = head ;
37
+ let length = 0 ;
38
+ while ( curr ) {
39
+ length ++ ;
40
+ curr = curr . next ;
41
+ }
42
+ return length ;
43
+ }
44
+
45
+ // 栈
46
+ function stackFn ( head ) {
47
+ let dummyNode = new ListNode ( - 1 ) ;
48
+ dummyNode . next = head ;
49
+ let curr = dummyNode ;
50
+ let stack = [ ] ;
51
+ // 入栈
52
+ while ( curr ) {
53
+ stack . push ( curr ) ;
54
+ curr = curr . next ;
55
+ }
56
+ // 出栈
57
+ for ( let i = 0 ; i < n ; i ++ ) {
58
+ stack . pop ( ) ;
59
+ }
60
+ // 待删除节点的前置节点
61
+ const prev = stack . at ( - 1 ) ;
62
+ prev . next = prev . next . next ;
63
+
64
+ return dummyNode . next ;
65
+ }
66
+
67
+ // 双指针
68
+ var twoPointers = function ( head , n ) {
69
+ let dummyNode = new ListNode ( - 1 ) ;
70
+ dummyNode . next = head ;
71
+
72
+ let fast = head ;
73
+ // 指向哑节点
74
+ let slow = dummyNode ;
75
+
76
+ // 前进 n 步
77
+ for ( let i = 0 ; i < n ; i ++ ) {
78
+ fast = fast . next ;
79
+ }
80
+ while ( fast ) {
81
+ fast = fast . next ;
82
+ slow = slow . next ;
83
+ }
84
+ // 此时,slow 指向待删除节点的前驱节点
85
+ slow . next = slow . next . next ;
86
+
87
+ return dummyNode . next ;
88
+ } ;
You can’t perform that action at this time.
0 commit comments