# Definition for singly-linked list.# class ListNode(object):# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclassSolution(object):defremoveNthFromEnd(self, head, n):"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""# 设置两个指针,一个指针指向的节点是另一个指针指向的节点的后n个# 当后一个指针抵达末尾时,前一个指针的下一个节点就是需要被删除的对象
front = head
for i inrange(n):
front = front.next# 删除头节点if front==None:return head.next
cur = head
while(front.next!=None):
front = front.next
cur = cur.next# front移动到了末尾
cur.next= cur.next.nextreturn head