From 013ba0575ed1729e8a1c31c251313e379d38adc2 Mon Sep 17 00:00:00 2001 From: evanlai Date: Sun, 30 May 2021 14:13:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200019.=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=ACN?= =?UTF-8?q?=E4=B8=AA=E8=8A=82=E7=82=B9=20python3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0019.删除链表的倒数第N个节点.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index 7d2fe97e..52735794 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -112,6 +112,30 @@ class Solution { } } ``` + +Python: +```python +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: + head_dummy = ListNode() + head_dummy.next = head + + slow, fast = head_dummy, head_dummy + while(n!=0): #fast先往前走n步 + fast = fast.next + n -= 1 + while(fast.next!=None): + slow = slow.next + fast = fast.next + #fast 走到结尾后,slow的下一个节点为倒数第N个节点 + slow.next = slow.next.next #删除 + return head_dummy.next +``` Go: ```Go /**