原题链接
typescript
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
// 双指针
const dummyHead = new ListNode(0, head);
let fast = head;
let slow = dummyHead;
for(let i = 0; i < n; i++) {
fast = fast?.next;
}
while(fast !== null) {
fast = fast?.next;
slow = slow?.next
}
const next = slow.next?.next;
slow.next = next;
return dummyHead.next;
};