File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed
remove-nth-node-from-end-of-list Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -582,6 +582,8 @@ https://leetcode.cn/problems/calculator-lcci
582
582
583
583
https://leetcode.cn/problems/longest-palindromic-substring
584
584
585
+ https://leetcode.cn/problems/remove-nth-node-from-end-of-list
586
+
585
587
#### 安装教程
586
588
587
589
1 . 安装` deno `
Original file line number Diff line number Diff line change
1
+ import { ListNode } from "../reverse-linked-list/ListNode.ts" ;
2
+ export default function removeNthFromEnd (
3
+ head : ListNode | null ,
4
+ n : number ,
5
+ ) : ListNode | null {
6
+ if ( ! head ) return head ;
7
+ const nodes : ListNode [ ] = [ ] ;
8
+
9
+ let cur : ListNode | null = head ;
10
+ while ( cur ) {
11
+ nodes . push ( cur ) ;
12
+ cur = cur . next ;
13
+ }
14
+
15
+ if ( nodes . length < n ) {
16
+ return null ;
17
+ }
18
+
19
+ if ( nodes . length === n ) {
20
+ return nodes [ 1 ] || null ;
21
+ }
22
+ if ( nodes . length > n ) {
23
+ const pre = nodes [ nodes . length - n - 1 ] ;
24
+ const succ = nodes [ nodes . length - n + 1 ] ;
25
+
26
+ if ( pre ) {
27
+ pre . next = succ || null ;
28
+ }
29
+ }
30
+ return head ;
31
+ }
You can’t perform that action at this time.
0 commit comments