File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode.cn id=19 lang=javascript
3
+ *
4
+ * [19] 删除链表的倒数第N个节点
5
+ */
6
+
7
+ // @lc code=start
8
+ /**
9
+ * Definition for singly-linked list.
10
+ * function ListNode(val) {
11
+ * this.val = val;
12
+ * this.next = null;
13
+ * }
14
+ */
15
+ /**
16
+ * @param {ListNode } head
17
+ * @param {number } n
18
+ * @return {ListNode }
19
+ */
20
+ var removeNthFromEnd = function ( head , n ) {
21
+ let a = head ;
22
+ let b = head ;
23
+ let t = n ;
24
+ while ( t && b . next ) {
25
+ b = b . next ;
26
+ t -- ;
27
+ }
28
+ while ( b . next ) {
29
+ a = a . next ;
30
+ b = b . next ;
31
+ }
32
+
33
+ if ( a === b ) {
34
+ return null ;
35
+ } else if ( t === 1 ) {
36
+ return a . next ;
37
+ } else if ( t === 2 ) {
38
+ return head . next ;
39
+ }
40
+
41
+ a . next = a . next . next ;
42
+
43
+ return head ;
44
+ } ;
45
+ // @lc code=end
46
+
You can’t perform that action at this time.
0 commit comments