添加 0019.删除链表的倒数第N个节点 python3版本

This commit is contained in:
evanlai
2021-05-30 14:13:38 +08:00
parent 5e1860876b
commit 013ba0575e

View File

@ -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
/**