Skip to content

Commit f432bc8

Browse files
committed
solution for remove nth node
1 parent d596ca5 commit f432bc8

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

leetcode_Python/remove_nth_node.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def removeNthFromEnd(self, head, n: int):
8+
fast = slow = head
9+
for _ in range(n):
10+
fast = fast.next
11+
12+
if not fast:
13+
return head.next
14+
15+
while fast.next:
16+
fast = fast.next
17+
slow = slow.next
18+
slow.next = slow.next.next
19+
return head
20+
21+
# LINK : https://leetcode.com/problems/remove-nth-node-from-end-of-list/

0 commit comments

Comments
 (0)