function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
let dummy = new ListNode(0); // Dummy node to handle edge cases
dummy.next = head;
let fast: ListNode | null = dummy;
let slow: ListNode | null = dummy;
// Advance 'fast' by 'n' nodes
for (let i = 0; i < n + 1; i++) {
fast = fast.next;
}
// Traverse the list until 'fast' reaches the end
while (fast !== null) {
fast = fast.next;
slow = slow.next;
}
// 'slow' is just before the Nth node, remove it
if (slow.next !== null) {
slow.next = slow.next.next;
}
return dummy.next; // Return the new head of the list
}