class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
dummy = ListNode(0, head) # A dummy node to handle edge cases
fast = slow = dummy # Initialize both pointers to the dummy node
# Advance 'fast' by 'n' nodes
for _ in range(n):
fast = fast.next
# Traverse the list until 'fast' reaches the end
while fast.next:
fast = fast.next
slow = slow.next
# 'slow' is now just before the Nth node, remove it
slow.next = slow.next.next
return dummy.next # Return the new head of the list