python实现删除链表倒数第n个节点

# 删除链表倒数第n个节点。假设n大于0
def remove_nth_from_end(head):
    fast = head
    count = 0
    while fast and count < n:
        fast = fast._next
        count += 1
    if not fast and count < n:  # not that many nodes
        return head
    if not fast and count == n:
        return head._next
    
    slow = head
    while fast._next:
        fast, slow = fast._next, slow._next
    slow._next = slow._next._next
    return head

 

posted @ 2021-07-12 18:15  水天需  阅读(77)  评论(0编辑  收藏  举报