【leetcode笔记】Python实现19. 删除链表的倒数第N个节点

更新:2019年05月21日20:27:53

法1.两边遍历,保存在list里

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        # 法1.数组法两边遍历 O(n^2) and O(n)
        res = []
        while head:
            res.append(head.val)
            head = head.next
        res = res[:-n] +  (res[-n+1:] if -n+1 < 0 else [])
        ans = ListNode(0)
        tmp = ans
        for i in res:
            tmp.next = ListNode(i)
            tmp = tmp.next
        return ans.next

法2.递归法,特别巧妙,利用递归从最后一个计数

class Solution:
    def removeNthFromEnd(self, head, n):
        def index(node):
            if not node:
                return 0
            i = index(node.next) + 1      # 由于是递归法,所以i是从最后一个往前加的
            if i > n:
                node.next.val = node.val  # 没有删除倒数第n个listnode,而是将值后移了
            return i
        index(head)
        return head.next

法3.快慢指针法

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
       # 法3.快慢指针
        fast = slow = head
        for _ in range(n):
            fast = fast.next
        if not fast:
            return head.next
        while fast.next:
            fast = fast.next          # 快指针剩余的个数就是慢 (总个数-n) 刚好是慢指针需要走的个数
            slow = slow.next
        slow.next = slow.next.next
        return head

参考:https://leetcode.com/problems/remove-nth-node-from-end-of-list/discuss/8802/3-short-Python-solutions

更多题解见我的github:https://github.com/JackKuo666/leetcode_notes

你可能感兴趣的:(leetcode,Python刷Leetcode)