Skip to content

Commit 013ba05

Browse files
committed
添加 0019.删除链表的倒数第N个节点 python3版本
1 parent 5e18608 commit 013ba05

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

problems/0019.删除链表的倒数第N个节点.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,30 @@ class Solution {
112112
}
113113
}
114114
```
115+
116+
Python:
117+
```python
118+
# Definition for singly-linked list.
119+
# class ListNode:
120+
# def __init__(self, val=0, next=None):
121+
# self.val = val
122+
# self.next = next
123+
class Solution:
124+
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
125+
head_dummy = ListNode()
126+
head_dummy.next = head
127+
128+
slow, fast = head_dummy, head_dummy
129+
while(n!=0): #fast先往前走n步
130+
fast = fast.next
131+
n -= 1
132+
while(fast.next!=None):
133+
slow = slow.next
134+
fast = fast.next
135+
#fast 走到结尾后,slow的下一个节点为倒数第N个节点
136+
slow.next = slow.next.next #删除
137+
return head_dummy.next
138+
```
115139
Go:
116140
```Go
117141
/**

0 commit comments

Comments
 (0)