✅ 🔥 0 ms Runtime Beats 100% User 🔥|| Step By Steps Solution ✅ ||  Easy to Understand  ✅🔥 ||
\u2705  IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE AT THE END \u2705 :\n\n### Intuition :\nThe task is to remove the nth node from the end of a singly linked list
\u2705  IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE AT THE END \u2705 :\n\n### Intuition :\nThe task is to remove the nth node from the end of a singly linked list in one pass. We can achieve this by maintaining a gap of `n` nodes between two pointers so that, when the first pointer reaches the end, the second pointer is right before the node we want to remove. This approach uses a dummy node to handle edge cases cleanly, such as when the head of the list needs to be removed.\n\n### Approach :\n1. **Dummy Node**: Introduce a dummy node pointing to the head of the list. This allows us to handle cases where the node to be removed is the head, as the dummy node provides a consistent "previous" node.\n2. **Two Pointers**: Use two pointers, `first` and `second`, both initially pointing to the dummy node.\n3. **Advance `first` Pointer**: Move the `first` pointer `n + 1` steps ahead of `second`. This will create a gap of `n` nodes between `first` and `second`.\n4. **Move Both Pointers**: Move both pointers one step at a time until `first` reaches the end of the list. At this point, `second` is right before the node to remove.\n5. **Remove the Target Node**: Adjust the `next` pointer of `second` to skip the target node.\n6. **Return the New Head**: Return `dummy.next` as the new head of the list.\n\n### Complexity Analysis :\n- **Time Complexity**: O(sz), where `sz` is the number of nodes in the list. We make a single pass through the list to find and remove the node.\n- **Space Complexity**: O(1), as we only use a few pointers and no additional space.\n\n### Code Implementations :\n\n#### Java Code :\n```java \nclass Solution {\n    public ListNode removeNthFromEnd(ListNode head, int n) {\n        ListNode dummy = new ListNode(0);  // Dummy node to simplify edge cases\n        dummy.next = head;\n        \n        ListNode first = dummy;\n        ListNode second = dummy;\n        \n        // Move `first` n+1 steps ahead to create the necessary gap\n        for (int i = 0; i <= n; i++) {\n            first = first.next;\n        }\n        \n        // Move both `first` and `second` until `first` reaches the end\n        while (first != null) {\n            first = first.next;\n            second = second.next;\n        }\n        \n        // Remove the nth node from the end\n        second.next = second.next.next;\n        \n        return dummy.next;  // Return the head of the modified list\n    }\n}\n```\n\n#### C++ Code :\n```cpp\n#include <iostream>\nusing namespace std;\n\nclass Solution {\npublic:\n    ListNode* removeNthFromEnd(ListNode* head, int n) {\n        ListNode* dummy = new ListNode(0);  // Dummy node to simplify edge cases\n        dummy->next = head;\n        \n        ListNode* first = dummy;\n        ListNode* second = dummy;\n        \n        // Move `first` n+1 steps ahead to create the necessary gap\n        for (int i = 0; i <= n; i++) {\n            first = first->next;\n        }\n        \n        // Move both `first` and `second` until `first` reaches the end\n        while (first != nullptr) {\n            first = first->next;\n            second = second->next;\n        }\n        \n        // Remove the nth node from the end\n        second->next = second->next->next;\n        \n        return dummy->next;  // Return the head of the modified list\n    }\n};\n```\n\n#### Python Code :\n```python\n\n\nclass Solution:\n    def removeNthFromEnd(self, head, n):\n        dummy = ListNode(0)  # Dummy node to simplify edge cases\n        dummy.next = head\n        \n        first = dummy\n        second = dummy\n        \n        # Move `first` n+1 steps ahead to create the necessary gap\n        for _ in range(n + 1):\n            first = first.next\n        \n        # Move both `first` and `second` until `first` reaches the end\n        while first:\n            first = first.next\n            second = second.next\n        \n        # Remove the nth node from the end\n        second.next = second.next.next\n        \n        return dummy.next  # Return the head of the modified list\n```\n\n![WhatsApp Image 2024-10-25 at 11.38.29.jpeg](https://assets.leetcode.com/users/images/8e7a65d5-685d-4027-b78c-969f2dc7d863_1729836651.7045465.jpeg)
['Linked List', 'Two Pointers', 'C++', 'Java', 'Python3']