Beats 100% of users with C++ || Using Two Pointer || Step by Step Explain || Fast Solution ||
---\n\n# Abhiraj Pratap Singh\n\n---\n\n\n# UPVOTE IT....\n\n\n---\n\n\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n\n- The rem
---\n\n# Abhiraj Pratap Singh\n\n---\n\n\n# UPVOTE IT....\n\n\n---\n\n\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n- The removeNthFromEnd function appears to remove the n-th node from the end of a singly-linked list.\n\n\n---\n\n\n![Screenshot (47).png](https://assets.leetcode.com/users/images/73fde954-77e9-4e0a-8f86-df7368eda527_1705080305.3989937.png)\n\n---\n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n1. Define a helper function length_ll to calculate the length of the linked list.\n2. Calculate the length of the linked list using the helper function.\n3. If the length is 1, return NULL since there is only one node, and removing it would make the list empty.\n4. If the length is equal to n, return head->next to remove the first node.\n5. Iterate through the list to reach the node before the one to be removed. Update the next pointer of this node to skip the n-th node.\n6. Return the modified head of the list.\n\n\n---\n\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- **Time complexity: O(n),** where n is the length of the linked list. The function iterates through the list once to calculate its length and then once more to remove the n-th node from the end.\n\n\n\n---\n\n\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n- **Space complexity: O(1),** The function uses a constant amount of extra space regardless of the size of the input linked list.\n\n\n---\n# Code\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    int length_ll(ListNode* head)\n    {\n        int count_node=0;\n        ListNode *ptr=head;\n        while(ptr!=NULL)\n        {\n            count_node++;\n            ptr=ptr->next;\n        }\n        return count_node;\n    }\n    ListNode* removeNthFromEnd(ListNode* head, int n) \n    {\n        int len=length_ll(head);\n        if(len==1)\n            return NULL;\n        if(len==n)\n            return head->next;\n        int node_number=0;\n        ListNode *ptr=head;\n        while( node_number < len-n-1 )\n        {\n            ptr=ptr->next;\n            node_number++;\n        }\n        ptr->next=ptr->next->next;\n        return head;\n    }\n};\n```\n\n\n---\n\n---\n\n\n# if you like the solution please UPVOTE it....\n\n\n\n---\n![discord-discordgifemoji.gif](https://assets.leetcode.com/users/images/9561fa84-e35c-471a-a374-234404c9799a_1705080546.676081.gif)\n\n---\n\n\n![fucking-die-redditor-stupid-cuck-funny-cat.gif](https://assets.leetcode.com/users/images/b64178ee-3879-420b-a3fe-72577e749a9a_1705080550.0895352.gif)\n\n---\n---
['Linked List', 'Two Pointers', 'C++']