C++ Simplest Solution | One Pass | Two Pointer Technique
To solve this problem in one-pass & O(n) space, we need to find a way to both reach the end of the linked list & reach the nth node from the end simultaneoulsy.
* To solve this problem in one-pass & O(n) space, we need to find a way to both reach the end of the linked list & reach the nth node from the end simultaneoulsy.\n* To do that, we initialize 2 pointers ```*fast``` & ```*slow``` both pointing to the head of the linked lits, the stagger the both by ```n``` nodes, so that ```fast``` is ```n``` nodes ahead of ```slow```.\n* Doing this will cause ```slow``` to reach the ```n```\'th node from the end at the same time that ```fast``` reaches the end.\n* Since we will need access to the node before the target node in order to remove the target node, we are going to loop until ```fast->next != NULL``` rather than until ```fast != null```, so that we stop one node earlier.\n* This method will be a problem when ```n``` is same as the no. of nodes in the linked list, which would make the first node the target node, and hence it\'s not possible to find the node before the target node. If that\'s the case, we can just return ```head->next```\n\n**Time Complexity :** O(N) - N = sz\n\n**Auxiliary Space :** O(1)\n\n**Code :**\n```\nclass Solution {\npublic:\n    ListNode* removeNthFromEnd(ListNode* head, int n) {\n        ListNode *fast = head, *slow = head;\n        \n        for(int i=0; i<n; i++) fast = fast->next;\n        \n        if(!fast) return head->next;\n        \n        while(fast->next) fast = fast->next, slow = slow->next;\n        \n        slow->next = slow->next->next;\n        \n        return head;\n    }\n};\n```\n\n**If you like my solution & explanation, please upvote my post :)**\n
['Two Pointers', 'C', 'C++']