classSolution:defremoveNthFromEnd(self , head , n ):
fast=head
newHead=ListNode(None)#创建头节点可以解决空链表的问题
newHead.next=head
pre=newHead
for i inrange(n):
fast=fast.nextwhile fast:
fast=fast.next
pre=pre.next
pre.next=pre.next.nextreturn newHead.next
力扣19牛客链接快慢指针法class Solution: def removeNthFromEnd(self , head , n ): fast=head newHead=ListNode(None)#创建头节点可以解决空链表的问题 newHead.next=head pre=newHead for i in range(n): fast=fast.next while f