Skip to content

Commit 6a90723

Browse files
committed
Create 19. Remove Nth Node From End of List.py
1 parent 60d4754 commit 6a90723

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2019/3/4 16:20
3+
# @Author : xulzee
4+
5+
# @File : 19. Remove Nth Node From End of List.py
6+
# @Software: PyCharm
7+
8+
# Definition for singly-linked list.
9+
class ListNode:
10+
def __init__(self, x):
11+
self.val = x
12+
self.next = None
13+
14+
15+
class Solution:
16+
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
17+
dummy_node = ListNode(0)
18+
dummy_node.next = head
19+
slow = dummy_node
20+
fast = dummy_node
21+
while (fast.next != None):
22+
if n <= 0:
23+
slow = slow.next
24+
fast = fast.next
25+
n -= 1
26+
slow.next = slow.next.next
27+
return dummy_node.next
28+
29+

0 commit comments

Comments
 (0)