Remove Nth Node from End of List

Linked List
Medium

Approach

Diagrams

Analysis

Time

O(n)

Space

O(1)
class Solution:
    def removeNthFromEnd(self, head, n):
        nodeBeforeHead = ListNode(None)
        nodeBeforeHead.next = head

        slow = fast = nodeBeforeHead

        for node in range(n):
            fast = fast.next

        while fast.next:
            slow = slow.next
            fast = fast.next

        slow.next = slow.next.next
        return nodeBeforeHead.next

Learn

#Linked List

Videos