# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
if not head:
return
dummy_node=ListNode(-1)
dummy_node.next=head
cur=dummy_node
lens=0
while cur.next:
lens+=1
cur=cur.next
if n>lens:
return
cur=dummy_node
for _ in range(lens-n):
cur=cur.next
cur.next=cur.next.next
return dummy_node.next