# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param head ListNode类 
# @param n int整型 
# @return ListNode类
#
class Solution:
    def removeNthFromEnd(self , head: ListNode, n: int) -> ListNode:
        # write code here
        slow = head
        fast = head
        for _ in range(n):
            fast = fast.next # 快指针先走n步
        # 快指针走到None,说明删除头节点
        if not fast:
            return head.next
        while fast.next:
            slow = slow.next
            fast = fast.next
		# slow 为要删除的节点的上节点
        slow.next = slow.next.next
        return head