LC 0019 [M] Remove Nth Node From End of List - ALawliet/algorithms GitHub Wiki
https://www.youtube.com/watch?v=XVuQxVej6y8&ab_channel=NeetCode
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
dum = ListNode(-1, head)
left = dum # start at -1
right = head
# increment right n times
while n > 0 and right:
right = right.next
n -= 1
# increment both left and right until right is null
while right:
right = right.next
left = left.next
# delete by skipping over one
left.next = left.next.next
# l n
# 1 -> 2 -> 3
return dum.next