指定头结点的前一个节点为None,赋值给pre,当前头结点赋值给fast指针
利用快慢指针找到删除节点的前一个节点
class Solution: def removeNthFromEnd(self , head , n ): # write code here fast = head newHead = ListNode(None) newHead.next = head pre = newHead for i in range(n): fast = fast.next while fast is not None: fast = fast.next pre = pre.next # del pre next pre.next = pre.next.next return newHead.next