Fast & slow pointers Care with memory leaks|0ms  beats 100%
Intuition\n Describe your first thoughts on how to solve this problem. \nThe fast pointer is n steps away from the slow.\n# Approach\n Describe your approach to
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe fast pointer is n steps away from the slow.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n[Please turn on English subtitles if necesssary]\n[https://youtu.be/OqLR3ShDALY?si=Pz17e_fjYog6zCDr](https://youtu.be/OqLR3ShDALY?si=Pz17e_fjYog6zCDr)\n1. Let fast go n moves\n2. Move slow & fast step by step until fast goes to the end\n3. Remove the nth node from the end\n\nThe code is done several months ago, C, C++ codes without memory leaks are made.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(|*head|)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$\n# Code\n```Python3 []\n# Definition for singly-linked list.\n# class ListNode:\n#     def __init__(self, val=0, next=None):\n#         self.val = val\n#         self.next = next\nclass Solution:\n    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:\n        fast=head\n        slow=head\n        for _ in range(n):\n            fast=fast.next\n        if not fast: return head.next\n        while fast.next:\n            fast=fast.next\n            slow=slow.next\n        slow.next=slow.next.next\n        return head\n        \n```\n\n```C++ []\n\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode() : val(0), next(nullptr) {}\n *     ListNode(int x) : val(x), next(nullptr) {}\n *     ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n    ListNode* removeNthFromEnd(ListNode* head, int n) {\n        ListNode* fast = head;\n        ListNode* slow = head;\n        \n        // Move the fast pointer n positions ahead\n        for (int i = 0; i < n; i++) {\n            fast = fast->next;\n        }\n        \n        // Handle the case when the first element is to be removed\n        if (fast == NULL) {\n            return head->next;\n        }\n        \n        // Move both pointers until the fast pointer reaches the end\n        while (fast->next) {\n            fast = fast->next;\n            slow = slow->next;\n        }\n        \n        // Remove the nth node from the end\n        slow->next = slow->next->next;\n        \n        return head;\n    }\n};\n\n```\n# C, C++ code without memory leaks||C 0ms  beats 100%\n```C []\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     struct ListNode *next;\n * };\n */\n#pragma GCC optimize("O3", "unroll-loops")\nstruct ListNode* removeNthFromEnd(struct ListNode* head, int n) {\n    struct ListNode* fast=head, *slow=head;\n    for(register i=0; i<n; i++) fast=fast->next;\n    if (!fast) {\n        struct ListNode* newHead=head->next;\n        free(head); //avoid of memory leaks\n        return newHead;\n    }\n    while(fast->next){\n        fast=fast->next, slow=slow->next;\n    }\n    struct ListNode *node= slow->next;\n    slow->next = slow->next->next;\n    free(node);//avoid of memory leaks\n    return head;\n}\n```\n\n```C++ []\nclass Solution {\npublic:\n    ListNode* removeNthFromEnd(ListNode* head, int n) {\n        ListNode* fast = head;\n        ListNode* slow = head;\n        \n        // Move the fast pointer n positions ahead\n        for (int i = 0; i < n; i++) {\n            fast = fast->next;\n        }\n        \n        // Handle the case when the first element is to be removed\n        if (fast == NULL) {\n            ListNode* newHead=head->next;\n            delete head; //avoid of memory leaks\n            return newHead;\n        }\n        \n        // Move both pointers until the fast pointer reaches the end\n        while (fast->next) {\n            fast = fast->next;\n            slow = slow->next;\n        }\n        \n        // Remove the nth node from the end\n        ListNode *node= slow->next;\n        slow->next = slow->next->next;\n        delete node;//avoid of memory leaks\n        \n        return head;\n    }\n};\n\n```
['Linked List', 'Two Pointers', 'C', 'C++', 'Python3']