Algobase
Problems
Get Premium
Pricing
Problems
/
286. Remove Nth Node From End
Prev
Next
Visualizer
Problem
Solution
Code
Standard List (n=2)
Remove Head (n=5)
Remove Tail (n=1)
Single Node (n=1)
⌥
Visualizer will appear here
Problem
Solution
Code
1
def remove_nth_from_end(head, n):
2
dummy = ListNode(0, head)
3
slow = dummy; fast = dummy
4
for _ in range(n + 1): fast = fast.next
5
while fast is not None:
6
slow = slow.next; fast = fast.next
7
slow.next = slow.next.next
8
return dummy.next
Visualizer
Standard List (n=2)
Remove Head (n=5)
Remove Tail (n=1)
Single Node (n=1)
⌥
Visualizer will appear here