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

 

上一篇:[LeetCode]287. Find the Duplicate Number 图解Floyd判圈(龟兔赛跑)算法


下一篇:jquery动画