classSolution(object):defremoveNthFromEnd(self, head, n):"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
count=0
slow = head
fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
count+=1if n < count:if fast==None:
count -=1while count > n:
slow=slow.next
count-=1
slow.next=slow.next.nextelse:if fast ==None:
k =2*count - n
else:
k =2*count +1-n
if k==0:return head.nextelse:
p=head
while k>1:
p = p.next
k-=1
p.next= p.next.nextreturn head