deno.land / x / masx200_leetcode_test@10.6.5 / remove-nth-node-from-end-of-list / index.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { ListNode } from "../reverse-linked-list/ListNode.ts";
export default function removeNthFromEnd( head: ListNode | null, n: number,): ListNode | null { if (!head) return head; const nodes: ListNode[] = [];
let cur: ListNode | null = head; while (cur) { nodes.push(cur); cur = cur.next; }
if (nodes.length < n) { return null; }
if (nodes.length === n) { return nodes[1] || null; } if (nodes.length > n) { const pre = nodes[nodes.length - n - 1]; const succ = nodes[nodes.length - n + 1];
if (pre) { pre.next = succ || null; } } return head;}
masx200_leetcode_test

Version Info

Tagged at
a year ago