19. Remove Nth Node From End of List
solution
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
dummy = ListNode(next=head)
i = 0
slow = fast = dummy
while i < n:
fast = fast.next
i += 1
while fast.next is not None:
slow = slow.next
fast = fast.next
slow.next = slow.next.next
return dummy.nextfollow up
Last updated