Simple and fast C++ solution with explanation
\nclass Solution {\npublic:\n    ListNode* removeNthFromEnd(ListNode* head, int n) {\n        ListNode* slow = head;\n        ListNode* fast = head;\n        \n
```\nclass Solution {\npublic:\n    ListNode* removeNthFromEnd(ListNode* head, int n) {\n        ListNode* slow = head;\n        ListNode* fast = head;\n        \n\t\t// move fast pointer to the n + 1 element\n        while (n--) { fast = fast->next; }\n        \n        // handle edge case: given n is always valid, \n        // if fast reached the end, we need to remove the first element\n        if (fast == nullptr) return head->next;\n\n        // move both pointers at the same time maintaing the difference\n        while (fast->next != nullptr) {\n            fast = fast->next;\n            slow = slow->next;\n        }\n        \n        // slow will be pointing to the element before the one we want to remove\n        slow->next = slow->next->next;\n        \n        return head;\n    }\n};\n```
['C', 'C++']