Leetcode 19. Remove Nth Node From End of List(python)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 19. Remove Nth Node From End of List(python)相关的知识,希望对你有一定的参考价值。

链表操作,只能遍历一遍然后。

用双指针

# 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
        """
        left=right=head
        for i in range(n):
            right=right.next
        if right is None:   return head.next    #special
        while right.next is not None:
            right=right.next
            left=left.next
            
        left.next=left.next.next
        return head

  

以上是关于Leetcode 19. Remove Nth Node From End of List(python)的主要内容,如果未能解决你的问题,请参考以下文章

leetcode19. Remove Nth Node From End of List

LeetCode 19. Remove Nth Node From End of List

LeetCode-19-Remove Nth Node From End of List

leetcode 之Remove Nth Node From End of List(19)

LeetCode19- Remove Nth Node From End of List-Medium

LeetCode19- Remove Nth Node From End of List-Medium