Easy ,Beginner Friendly & Dry Run || Simple loops || Time O(N) Space O(1) || Gits ✅✅✅👈👈
\n\n# Intuition \uD83D\uDC48\n\nIn the given question, you are provided with the head of a linked list and a number n. You are required to remove the nth node f
![Screenshot 2024-02-07 164819.png](https://assets.leetcode.com/users/images/0b582f2d-9b4c-4f0d-aace-e0ec05ca4b49_1707308564.5210462.png)\n\n# Intuition \uD83D\uDC48\n\nIn the given question, you are provided with the head of a linked list and a number n. You are required to remove the nth node from the end of the list.\n\n# Approach \uD83D\uDC48\n\nTo solve this question, first, we need to determine the length of the linked list. After obtaining the length, we subtract n from it, which gives us the position of the nth node from the start that we need to remove from the linked list.\n\n# Code Explanation \uD83D\uDC48\n\n*   **Edge Case Handling**: The method first checks if the input `head` is `null` or if the list contains only one node (`head.next == null`). In either case, the method returns `null`, as removing a node from such a list would result in an empty list.\n    \n*   **Dummy Head Initialization**: A dummy node with a value of `0` is created and its `next` pointer is set to point to the `head` of the list. This dummy node acts as a placeholder to simplify edge cases, especially when the node to be removed is the head of the list.\n    \n*   **Counting Nodes**: The method iterates through the entire list starting from `head`, using a `while` loop to count the total number of nodes in the list. This count is stored in the variable `count`.\n    \n*   **Adjusting Count for Removal**: The variable `count` is then decremented by `n`. This adjustment translates the task from "remove the nth node from the end" to "find the (count\u2212n)th node from the beginning".\n    \n*   **Finding the Predecessor Node**: The method initializes a pointer `prev` to the dummy node and then advances it `count` times through the list. After this loop, `prev` points to the node immediately before the node that needs to be removed.\n    \n*   **Removing the Node**: The node to be removed is `prev.next`. To remove it, the method sets `prev.next` to `prev.next.next`, effectively bypassing the node to be removed and linking `prev` directly to the node after the one to be removed. This operation does not deallocate the removed node, but it effectively removes it from the list as seen by traversing the list from the dummy head.\n    \n*   **Returning the New Head**: Finally, the method returns `dummy.next`, which is the new head of the list. If the original head of the list was removed, `dummy.next` now points to the second node, which is the new head. If any other node was removed, the structure of the list is updated accordingly, with the original head still in place.\n\n# Complexity \uD83D\uDC48\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n\n# Code \uD83D\uDC48\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    if(head == NULL || head->next == NULL)return NULL;\n\n    struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode));\n    dummy -> next = head;\n    int count = 0;\n\n    struct ListNode* iterate = head;\n    while(iterate != NULL){\n        count++;\n        iterate = iterate->next;\n    }      \n    count-=n;\n    struct ListNode* prev = dummy; \n    for (int i = 0; i < count; i++) {\n        prev = prev->next;\n    }\n    prev->next = prev->next->next;\n    return dummy->next;\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        if(head == NULL || head->next == NULL)return NULL;\n\n        ListNode* dummy = new ListNode(0);\n        dummy->next = head;\n        int count = 0;\n\n        ListNode* iterate = head;\n        while(iterate != NULL){\n            count++;\n            iterate = iterate->next;\n        }      \n        count-=n;\n        ListNode* prev = dummy; \n        for (int i = 0; i < count; i++) {\n            prev = prev->next;\n        }\n        prev->next = prev->next->next;\n        return dummy->next;\n    }\n};\n```\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        if(head == null || head.next == null)return null;\n\n        ListNode dummy = new ListNode(0);\n        dummy.next = head;\n        int count = 0;\n\n        ListNode iterate = head;\n        while(iterate != null){\n            count++;\n            iterate = iterate.next;\n        }      \n        count-=n;\n        ListNode prev = dummy; \n        for (int i = 0; i < count; i++) {\n            prev = prev.next;\n        }\n        prev.next = prev.next.next;\n        return dummy.next;\n    }\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    if(head == null || head.next == null)return null;\n\n    dummy = new ListNode(0);\n    dummy.next = head;\n    let count = 0;\n\n    iterate = head;\n    while(iterate != null){\n        count++;\n        iterate = iterate.next;\n    }      \n    count-=n;\n    prev = dummy; \n    for (let i = 0; i < count; i++) {\n        prev = prev.next;\n    }\n    prev.next = prev.next.next;\n    return dummy.next;\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        if(head == null || head.next == null)return null;\n\n        ListNode dummy = new ListNode(0);\n        dummy.next = head;\n        int count = 0;\n\n        ListNode iterate = head;\n        while(iterate != null){\n            count++;\n            iterate = iterate.next;\n        }      \n        count-=n;\n        ListNode prev = dummy; \n        for (int i = 0; i < count; i++) {\n            prev = prev.next;\n        }\n        prev.next = prev.next.next;\n        return dummy.next;\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        """\n        :type head: ListNode\n        :type n: int\n        :rtype: ListNode\n        """\n        if head == None or head.next == None: return None\n\n        dummy = ListNode(0)\n        dummy.next = head\n        count = 0\n\n        iterate = head\n        while iterate != None:\n            count+=1\n            iterate = iterate.next \n\n        count-=n\n        prev = dummy\n        for i in range(0 , count):\n            prev = prev.next\n\n        prev.next = prev.next.next\n        return dummy.next\n```\n```ruby []\n# Definition for singly-linked list.\n# class ListNode\n#     attr_accessor :val, :next\n#     def initialize(val = 0, _next = nil)\n#         @val = val\n#         @next = _next\n#     end\n# end\n# @param {ListNode} head\n# @param {Integer} n\n# @return {ListNode}\ndef remove_nth_from_end(head, n)\n    if head == nil or head.next == nil \n        return nil\n    end\n    dummy = ListNode.new(0)\n    dummy.next = head\n    count = 0\n\n    iterate = head\n    while iterate != nil\n        count+=1\n        iterate = iterate.next \n    end\n    count-=n\n    prev = dummy\n    for a in 1..count do\n        prev = prev.next\n    end\n\n    prev.next = prev.next.next\n    return dummy.next\nendus aacha \n```\n\n# Dry Run \uD83D\uDC48\n\n1.  **Initialization**:\n    \n    *   The list is `[1,2,3,4,5]`.\n    *   `n = 2`, meaning we want to remove the 2nd node from the end of the list, which is `4`.\n2.  **Dummy Node**:\n    \n    *   A dummy node is created and its `next` is set to point to the head of the list. Now, the list looks like `[dummy,1,2,3,4,5]`.\n3.  **Counting Nodes**:\n    \n    *   The method iterates through the list to count the nodes. `count` becomes `5` because there are 5 nodes in the list.\n4.  **Adjusting Count for Removal**:\n    \n    *   The method calculates the position to stop before the node to be removed: `count = count - n`, which is `5 - 2 = 3`. This means we need to find the 3rd node from the beginning, which is `3`, to get to its predecessor.\n5.  **Finding the Predecessor Node**:\n    \n    *   Starting from the dummy node, the method advances `prev` 3 times. After these iterations, `prev` points to the node `3`.\n6.  **Removing the Node**:\n    \n    *   `prev.next` points to `4`, and `prev.next.next` points to `5`. The method sets `prev.next` to `prev.next.next`, which makes `prev.next` point to `5`. Now, the list looks like `[dummy,1,2,3,5]`, effectively removing the `4`.\n7.  **Returning the New Head**:\n    \n    *   Finally, the method returns `dummy.next`, which is the head of the new list `[1,2,3,5]`.\n\n# Here\'s a more detailed breakdown of the key steps: \uD83D\uDC48\n\n*   **Before Removal**:\n    \n    *   List: `Dummy -> 1 -> 2 -> 3 -> 4 -> 5`\n*   **After Counting (`count = 3` after adjustment)**:\n    \n    *   `prev` starts at `Dummy` and moves to `3` after 3 iterations.\n*   **Node Removal**:\n    \n    *   Before: `Dummy -> 1 -> 2 -> 3 -> 4 -> 5`\n    *   After: `Dummy -> 1 -> 2 -> 3 ------> 5`\n*   **Result**:\n    \n    *   The final list returned is `[1,2,3,5]`, with the node containing `4` removed.\n\n# Hey Guys if you like the explanation and solution the upvote me.\u2764\uFE0F\u2764\uFE0F\u2764\uFE0F\n
['Linked List', 'Two Pointers', 'C', 'Python', 'C++', 'Java', 'Python3', 'Ruby', 'JavaScript', 'C#']