class Solution(object):
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
h=ListNode(-1)
h.next=head
p,q = h,h
for _ in range(n+1): #如删除头结点,q要直接走到None
q=q.next
while q!=None:
p = p.next
q = q.next
p.next = p.next.next
return h.next
删除链表倒数第N个结点 python
最新推荐文章于 2022-08-14 16:44:38 发布