# 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 , n ):
        # write code here
        count = 0
        cur = head
        while cur:
            count += 1
            cur = cur.next
        cur = head
        if n==count: return head.next
        
        for i in range(count-n-1):
            cur = cur.next
        if not cur or not cur.next: return head
        cur.next = cur.next.next
        return head