class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        if head:
            slow = fast = prev = head
            slow_count = fast_count = 0
            
            while fast:
                fast = fast.next
                fast_count += 1
                
                if (fast_count - slow_count) > n:
                    prev = slow
                    slow = slow.next
                    slow_count += 1
            
            if prev == slow:
                head = prev.next
            else:
                prev.next = slow.next
        
        return head
99059e830c1b212c5f2516c35833edc853cc41da
<mask token>

class Solution:
    <mask token>

class Solution:

    def removeNthFromEnd(self, head: ListNode, n: int) ->ListNode:
        if head:
            slow = fast = prev = head
            slow_count = fast_count = 0
            while fast:
                fast = fast.next
                fast_count += 1
                if fast_count - slow_count > n:
                    prev = slow
                    slow = slow.next
                    slow_count += 1
            if prev == slow:
                head = prev.next
            else:
                prev.next = slow.next
        return head

class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        if head:
            slow = fast = prev = head
            slow_count = fast_count = 0
            
            while fast:
                fast = fast.next
                fast_count += 1
                
                if (fast_count - slow_count) > n:
                    prev = slow
                    slow = slow.next
                    slow_count += 1
            
            if prev == slow:
                head = prev.next
            else:
                prev.next = slow.next
        
        return head