python递归 class Solution: def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: pos = self.findpos(head,n) #计算head到结尾共有多少个节点 if pos == n: #需要删除头节点 return head.next return head def findpos(self,node,n): if node == None: return 0 pos = self.findpos(node.next,n) + 1 #递归,从最后一个节点开始计数,每个节点加1 if pos == n+1: #当前节点是需要删除的前一个节点 node.next = node.next.next return pos python双指针 class Solution: def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: p = node = head i = 0 while(node.next): node = node.next if i < n: i += 1 continue p = p.next if i <n: return p.next else: p.next = p.next.next return head