🔥✅✅ Beats 100% with Proof | Very Easy to Understand ✅✅🔥
Proof - Upvote if you watching the proof\n\n\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \nGiven the head of a linked list, remo
# Proof - Upvote if you watching the proof\n\n![image.png](https://assets.leetcode.com/users/images/bcef3971-3f53-469b-a034-4c4dbab2532c_1709431080.2955086.png)\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nGiven the head of a linked list, remove the nth node from the end of the list and return its head.\n![image.png](https://assets.leetcode.com/users/images/02a78e05-84b6-4c53-b3bd-73ebf4ea0af6_1698291562.3217883.png)\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1) Calculate the size of the Single Linked List. We need to travel to the prev node of the node to be removed thus we perform reduce size by n\n2) If the node to be removed is the first node (size == 0) then we can simply return the next node of head since it will be null if the list has only one node.\n3) Traverse till the prev node using a loop again\n4) Skip the next node by linking the prev node to the next of next node. If not present, assign null. \n5. Finally return the head. \n\n# Complexity\n- Time complexity: O(N) \n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```Java []\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode() {}\n *     ListNode(int val) { this.val = val; }\n *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n * }\n */\nclass Solution {\n    public ListNode removeNthFromEnd(ListNode head, int n) {\n        int length = findLength(head);\n        int i = 0, traverseTill = length - n - 1;\n        if(traverseTill == -1) return head.next;\n        ListNode curr = head;\n        while(i < traverseTill){\n            curr = curr.next;\n            i++;\n        }\n        curr.next = curr.next.next;\n        return head;\n    }\n    public int findLength(ListNode head){\n        int count = 0;\n        if(head == null) return count;\n        ListNode curr = head;\n        while(curr != null){\n            count++;\n            curr = curr.next;\n        }\n        return count;\n    }\n}\n```\n```C []\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     struct ListNode *next;\n * };\n */\nstruct ListNode* removeNthFromEnd(struct ListNode* head, int n) {\n    int length = 0;\n    struct ListNode* curr = head;\n    \n    // Calculate the length of the linked list\n    while (curr != NULL) {\n        length++;\n        curr = curr->next;\n    }\n\n    // Find the position to remove\n    int traverseTill = length - n - 1;\n    int i = 0;\n    curr = head;\n\n    // If the head needs to be removed\n    if (traverseTill == -1) {\n        head = head->next;\n        free(curr);\n        return head;\n    }\n\n    // Traverse to the node before the one to be removed\n    while (i < traverseTill) {\n        curr = curr->next;\n        i++;\n    }\n\n    // Remove the nth node from the end\n    struct ListNode* temp = curr->next;\n    curr->next = curr->next->next;\n    free(temp);\n\n    return head;\n}\n\n```\n```C++ []\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        int length = 0;\n        ListNode* curr = head;\n        \n        // Calculate the length of the linked list\n        while (curr != nullptr) {\n            length++;\n            curr = curr->next;\n        }\n\n        // Find the position to remove\n        int traverseTill = length - n - 1;\n        int i = 0;\n        curr = head;\n\n        // If the head needs to be removed\n        if (traverseTill == -1) {\n            head = head->next;\n            delete curr;\n            return head;\n        }\n\n        // Traverse to the node before the one to be removed\n        while (i < traverseTill) {\n            curr = curr->next;\n            i++;\n        }\n\n        // Remove the nth node from the end\n        ListNode* temp = curr->next;\n        curr->next = curr->next->next;\n        delete temp;\n\n        return head;\n    }\n};\n\n```\n```C# []\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     public int val;\n *     public ListNode next;\n *     public ListNode(int val=0, ListNode next=null) {\n *         this.val = val;\n *         this.next = next;\n *     }\n * }\n */\npublic class Solution {\n    public ListNode RemoveNthFromEnd(ListNode head, int n) {\n        int length = 0;\n        ListNode curr = head;\n        \n        // Find the length of the linked list\n        while (curr != null) {\n            length++;\n            curr = curr.next;\n        }\n        \n        int traverseTill = length - n - 1;\n        curr = head;\n        \n        // Traverse to the node before the one to be removed\n        for (int i = 0; i < traverseTill; i++) {\n            curr = curr.next;\n        }\n        \n        // Remove the nth node from the end\n        if (traverseTill == -1) {\n            return head.next;\n        } else {\n            curr.next = curr.next.next;\n            return head;\n        }\n    }\n}\n\n```\n\n```Python []\n# Definition for singly-linked list.\n# class ListNode(object):\n#     def __init__(self, val=0, next=None):\n#         self.val = val\n#         self.next = next\nclass Solution(object):\n    def removeNthFromEnd(self, head, n):\n        length = self.findLength(head)\n        i, traverseTill = 0, length - n - 1\n        if traverseTill == -1:\n            return head.next\n        curr = head\n        while i < traverseTill:\n            curr = curr.next\n            i += 1\n        curr.next = curr.next.next\n        return head\n\n    def findLength(self, head):\n        count = 0\n        if head is None:\n            return count\n        curr = head\n        while curr is not None:\n            count += 1\n            curr = curr.next\n        return count\n\n```\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        length = self.findLength(head)\n        i, traverseTill = 0, length - n - 1\n        if traverseTill == -1:\n            return head.next\n        curr = head\n        while i < traverseTill:\n            curr = curr.next\n            i += 1\n        curr.next = curr.next.next\n        return head\n\n    def findLength(self, head):\n        count = 0\n        if head is None:\n            return count\n        curr = head\n        while curr is not None:\n            count += 1\n            curr = curr.next\n        return count\n\n```\n```Javascript []\n/**\n * Definition for singly-linked list.\n * function ListNode(val, next) {\n *     this.val = (val===undefined ? 0 : val)\n *     this.next = (next===undefined ? null : next)\n * }\n */\n/**\n * @param {ListNode} head\n * @param {number} n\n * @return {ListNode}\n */\nvar removeNthFromEnd = function (head, n) {\n    let length = findLength(head);\n    let i = 0,\n        traverseTill = length - n - 1;\n    if (traverseTill === -1) return head.next;\n    let curr = head;\n    while (i < traverseTill) {\n        curr = curr.next;\n        i++;\n    }\n    curr.next = curr.next.next;\n    return head;\n\n    function findLength(head) {\n        let count = 0;\n        if (head === null) return count;\n        let curr = head;\n        while (curr !== null) {\n            count++;\n            curr = curr.next;\n        }\n        return count;\n    }\n};\n\n```\n```Typescript []\n/**\n * Definition for singly-linked list.\n * class ListNode {\n *     val: number\n *     next: ListNode | null\n *     constructor(val?: number, next?: ListNode | null) {\n *         this.val = (val===undefined ? 0 : val)\n *         this.next = (next===undefined ? null : next)\n *     }\n * }\n */\n\nfunction removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {\n    let length = findLength(head);\n    let i = 0,\n        traverseTill = length - n - 1;\n    if (traverseTill === -1) return head.next;\n    let curr = head;\n    while (i < traverseTill) {\n        curr = curr.next;\n        i++;\n    }\n    curr.next = curr.next.next;\n    return head;\n\n    function findLength(head: ListNode | null): number {\n        let count = 0;\n        if (head === null) return count;\n        let curr = head;\n        while (curr !== null) {\n            count++;\n            curr = curr.next;\n        }\n        return count;\n    }\n}\n\n```\n# Upvote\n![image.png](https://assets.leetcode.com/users/images/44231f66-7f45-4243-8090-39ed84d288fe_1698291546.031606.png)\n\n
['Linked List', 'Two Pointers', 'C', 'Python', 'C++', 'Java', 'TypeScript', 'Python3', 'JavaScript', 'C#']