19,删除链表的倒数第N个节点

class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        tmp = head
        while tmp:
            if tmp == head:
                last = None
            new_tmp = tmp
            for _ in range(n):
                new_tmp = new_tmp.next
            if not new_tmp:
                if last == None:
                    return head.next
                else:
                    last.next = tmp.next
                    return head
            else:
                last = tmp
                tmp = tmp.next

猜你喜欢

转载自blog.csdn.net/weixin_42758299/article/details/88587830