{"id":783,"date":"2023-12-07T13:00:00","date_gmt":"2023-12-07T13:00:00","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=783"},"modified":"2023-12-08T13:06:09","modified_gmt":"2023-12-08T13:06:09","slug":"remove-nth-node-from-end-of-list","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/remove-nth-node-from-end-of-list\/","title":{"rendered":"Remove Nth Node From End of List (C++, Java, Python)"},"content":{"rendered":"\n<p>A linked list operates as a chain of connected elements, with each element aware of the next one in line. In this article, we will explore different approaches to remove the nth node from the end of a linked list with implementation in C++, Java, and Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Removing the nth Node from the End of the List<\/strong><\/h2>\n\n\n\n<p>When we discuss removing the nth element from the end of the linked list, we&#8217;re essentially targeting the element positioned at the nth spot when counting backward from the end of the list. In simpler terms, it&#8217;s like removing the element that holds the nth position from the list&#8217;s tail. <\/p>\n\n\n\n<p><strong>In this problem, we are given a linked list and a value of n and we have to remove a node that holds the nth position from the list\u2019s tail.<\/strong><\/p>\n\n\n\n<p>Let us understand it better with an example:<\/p>\n\n\n\n<p class=\"has-black-color has-text-color has-background\" style=\"background-color:#fedcba\">List nodes = [1,2,3,4,5,6]<br>n = 2<br>Output = [1,2,3,4,6]<\/p>\n\n\n\n<p>The second node from the end has a value of 5, we will remove that node. So our output list becomes [1,2,3,4,6].<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"373\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/1-4-1024x373.png\" alt=\"Example of Remove Nth Node From End of List\" class=\"wp-image-788\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/1-4-1024x373.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/1-4-300x109.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/1-4-768x280.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/1-4-750x273.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/1-4-1140x415.png 1140w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/1-4.png 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>Brute Force Approach<\/strong><\/h2>\n\n\n\n<p>In the brute force approach to solve this problem, we have to first count the length of the linked list for this we have to traverse a linked list once to count the length and traverse again to delete the nth node, nth node from the last is the length &#8211; nth node from starting.<\/p>\n\n\n\n<p>Steps to solve this problem:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the first step, we will initialize a variable length to 0 and use a while loop to traverse the linked list and count the number of nodes. After this loop, the length will contain the total number of nodes in the linked list.<\/li>\n\n\n\n<li>In the second step, we will create a dummy node with a value of 0 and set its next pointer to the head of the original linked list. This dummy node is used to simplify handling edge cases where the head of the list needs to be removed.&nbsp;<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"215\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/2-4-1024x215.png\" alt=\"create a dummy node\" class=\"wp-image-789\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/2-4-1024x215.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/2-4-300x63.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/2-4-768x161.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/2-4-750x157.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/2-4-1140x239.png 1140w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/2-4.png 1279w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>In this step, we will use a&nbsp; loop to move the current pointer to the node just before the one to be removed. The loop runs for length &#8211; n iterations.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"208\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/3-4-1024x208.png\" alt=\"move the current pointer\" class=\"wp-image-790\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/3-4-1024x208.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/3-4-300x61.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/3-4-768x156.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/3-4-750x152.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/3-4-1140x232.png 1140w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/3-4.png 1279w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>Finally, we will adjust the next pointer of the current node to skip over the nth node, effectively removing it from the linked list.&nbsp;<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"227\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/4-2-1024x227.png\" alt=\"adjust the next pointer of the current node\" class=\"wp-image-791\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/4-2-1024x227.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/4-2-300x66.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/4-2-768x170.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/4-2-750x166.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/4-2-1140x252.png 1140w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/4-2.png 1279w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p>The time complexity of the brute force approach is O(n), where n is the number of nodes in the linked list. The space complexity is O(1) as we haven\u2019t used any additional space.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Two Pointer Approach<\/strong><\/h2>\n\n\n\n<p>We can avoid the hassle of counting the length of the list by using the two-pointers. <strong>Two-Pointers are a good approach to removing the nth node from the end of a linked list.<\/strong> Let&#8217;s see the steps of how we can solve this problem:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We will create a new ListNode named \u2018dummy\u2019 with an empty value. Set the next \u2018dummy\u2019 to the head of the linked list.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"185\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/5-1-1024x185.png\" alt=\"create a dummy node\" class=\"wp-image-792\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/5-1-1024x185.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/5-1-300x54.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/5-1-768x139.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/5-1-750x135.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/5-1-1140x206.png 1140w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/5-1.png 1279w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>Now we will create two pointers, &#8216;fast&#8217; and &#8216;slow&#8217;, both initially pointing to \u2018dummy\u2019.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"200\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/6-1-1024x200.png\" alt=\"create two pointers, 'fast' and 'slow'\" class=\"wp-image-793\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/6-1-1024x200.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/6-1-300x59.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/6-1-768x150.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/6-1-750x146.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/6-1-1140x223.png 1140w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/6-1.png 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>Now we will Move the &#8216;fast&#8217; pointer n steps ahead in the linked list.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"220\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/7-1-1024x220.png\" alt=\"Move the 'fast' pointer n steps\" class=\"wp-image-794\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/7-1-1024x220.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/7-1-300x65.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/7-1-768x165.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/7-1-750x161.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/7-1-1140x245.png 1140w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/7-1.png 1279w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>Now we will&nbsp;Move both &#8216;slow&#8217; and &#8216;fast&#8217; pointers until the fast\u2019s next is not NULL<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"202\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/8-1-1024x202.png\" alt=\"Move both 'slow' and 'fast' pointers\" class=\"wp-image-795\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/8-1-1024x202.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/8-1-300x59.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/8-1-768x152.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/8-1-750x148.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/8-1-1140x225.png 1140w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/8-1.png 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>Now we will Remove the nth node from the end by adjusting pointers.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"221\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/9-1-1024x221.png\" alt=\"Remove the nth node from the end by adjusting pointers\" class=\"wp-image-796\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/9-1-1024x221.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/9-1-300x65.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/9-1-768x166.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/9-1-750x162.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/9-1-1140x246.png 1140w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/12\/9-1.png 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>C++ Code<\/strong><\/h3>\n\n\n\n<p>Below is the C++ program to remove the nth node from end of a list:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;C++&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n\n\/\/ Definition for singly-linked list.\nstruct ListNode {\n    int val;\n    ListNode* next;\n    ListNode(int x) : val(x), next(nullptr) {}\n};\n\nListNode* removeNthFromEnd(ListNode* head, int n) {\n    \/\/ Step 1: Create a new ListNode named 'start' with an empty value.\n    ListNode* start = new ListNode(0);\n\n    \/\/ Set the next 'start' to the head of the linked list.\n    start-&gt;next = head;\n\n    \/\/ Step 2: Create two pointers, 'fast' and 'slow', both initially pointing to 'start'.\n    ListNode* fast = start;\n    ListNode* slow = start;\n\n    \/\/ Step 3: Move the 'fast' pointer n steps ahead in the linked list.\n    for (int i = 0; i &lt; n; i++) {\n        fast = fast-&gt;next;\n    }\n\n    \/\/ Step 4: Move both 'slow' and 'fast' pointers until 'fast' reaches the end.\n    while (fast-&gt;next != nullptr) {\n        slow = slow-&gt;next;\n        fast = fast-&gt;next;\n    }\n\n    \/\/ Step 5: Remove the nth node from the end by adjusting pointers.\n    slow-&gt;next = slow-&gt;next-&gt;next;\n\n    \/\/ Step 6: Return the modified linked list starting from 'start-&gt;next'.\n    return start-&gt;next;\n}\n\n\/\/ Function to print the linked list\nvoid printList(ListNode* head) {\n    while (head != nullptr) {\n        std::cout &lt;&lt; head-&gt;val &lt;&lt; &quot; &quot;;\n        head = head-&gt;next;\n    }\n    std::cout &lt;&lt; std::endl;\n}\n\nint main() {\n    \/\/ Creating a sample linked list: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; 6\n    ListNode* head = new ListNode(1);\n    head-&gt;next = new ListNode(2);\n    head-&gt;next-&gt;next = new ListNode(3);\n    head-&gt;next-&gt;next-&gt;next = new ListNode(4);\n    head-&gt;next-&gt;next-&gt;next-&gt;next = new ListNode(5);\n    head-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = new ListNode(6);\n\n    std::cout &lt;&lt; &quot;Original Linked List: &quot;;\n    printList(head);\n\n    int n = 2; \/\/ Example: Remove the 2nd node from the end\n\n    \/\/ Remove the nth node from the end\n    head = removeNthFromEnd(head, n);\n\n    std::cout &lt;&lt; &quot;Modified Linked List: &quot;;\n    printList(head);\n\n    return 0;\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Python Code<\/strong><\/h3>\n\n\n\n<p>Here is the Python program for our problem:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">class ListNode:\n    def __init__(self, x):\n        self.val = x\n        self.next = None\n\ndef removeNthFromEnd(head, n):\n    # Step 1: Create a new ListNode named 'start' with an empty value.\n    start = ListNode(0)\n    # Set the next of 'start' to the head of the linked list.\n    start.next = head\n\n    # Step 2: Create two pointers, 'fast' and 'slow', both initially pointing to 'start'.\n    fast = start\n    slow = start\n\n    # Step 3: Move the 'fast' pointer n steps ahead in the linked list.\n    for _ in range(n):\n        fast = fast.next\n\n    # Step 4: Move both 'slow' and 'fast' pointers until 'fast' reaches the end.\n    while fast.next is not None:\n        slow = slow.next\n        fast = fast.next\n\n    # Step 5: Remove the nth node from the end by adjusting pointers.\n    slow.next = slow.next.next\n\n    # Step 6: Return the modified linked list starting from 'start.next'.\n    return start.next\n\n# Function to print the linked list\ndef printList(head):\n    while head is not None:\n        print(head.val, end=&quot; &quot;)\n        head = head.next\n    print()\n\n# Creating a sample linked list: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; 6\nhead = ListNode(1)\nhead.next = ListNode(2)\nhead.next.next = ListNode(3)\nhead.next.next.next = ListNode(4)\nhead.next.next.next.next = ListNode(5)\nhead.next.next.next.next.next = ListNode(6)\n\nprint(&quot;Original Linked List:&quot;, end=&quot; &quot;)\nprintList(head)\n\nn = 2  # Example: Remove the 2nd node from the end\n\n# Remove the nth node from the end\nhead = removeNthFromEnd(head, n)\n\nprint(&quot;Modified Linked List:&quot;, end=&quot; &quot;)\nprintList(head)<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Java Code<\/strong><\/h3>\n\n\n\n<p>You can implement it in Java too:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Java&quot;,&quot;modeName&quot;:&quot;java&quot;}\">class ListNode {\n    int val;\n    ListNode next;\n\n    ListNode(int x) {\n        val = x;\n        next = null;\n    }\n}\n\npublic class RemoveNthFromEnd {\n\n    public static ListNode removeNthFromEnd(ListNode head, int n) {\n        \/\/ Step 1: Create a new ListNode named 'start' with an empty value.\n        ListNode start = new ListNode(0);\n        \/\/ Set the next of 'start' to the head of the linked list.\n        start.next = head;\n\n        \/\/ Step 2: Create two pointers, 'fast' and 'slow', both initially pointing to 'start'.\n        ListNode fast = start;\n        ListNode slow = start;\n\n        \/\/ Step 3: Move the 'fast' pointer n steps ahead in the linked list.\n        for (int i = 0; i &lt; n; i++) {\n            fast = fast.next;\n        }\n\n        \/\/ Step 4: Move both 'slow' and 'fast' pointers until 'fast' reaches the end.\n        while (fast.next != null) {\n            slow = slow.next;\n            fast = fast.next;\n        }\n\n        \/\/ Step 5: Remove the nth node from the end by adjusting pointers.\n        slow.next = slow.next.next;\n\n        \/\/ Step 6: Return the modified linked list starting from 'start.next'.\n        return start.next;\n    }\n\n    \/\/ Function to print the linked list\n    public static void printList(ListNode head) {\n        while (head != null) {\n            System.out.print(head.val + &quot; &quot;);\n            head = head.next;\n        }\n        System.out.println();\n    }\n\n    public static void main(String[] args) {\n        \/\/ Creating a sample linked list: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; 6\n        ListNode head = new ListNode(1);\n        head.next = new ListNode(2);\n        head.next.next = new ListNode(3);\n        head.next.next.next = new ListNode(4);\n        head.next.next.next.next = new ListNode(5);\n        head.next.next.next.next.next = new ListNode(6);\n\n        System.out.print(&quot;Original Linked List: &quot;);\n        printList(head);\n\n        int n = 2; \/\/ Example: Remove the 2nd node from the end\n\n        \/\/ Remove the nth node from the end\n        head = removeNthFromEnd(head, n);\n\n        System.out.print(&quot;Modified Linked List: &quot;);\n        printList(head);\n    }\n}<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original Linked List: 1 2 3 4 5 6&nbsp;\nModified Linked List: 1 2 3 4 6<\/code><\/pre>\n\n\n\n<p>The time complexity of this approach is O(n) as the code traverses the list once using two pointers (fast and slow) until the fast reaches the end. Here n is the number of nodes in the linked list.<\/p>\n\n\n\n<p>The space complexity is O(1) as we haven\u2019t used any extra space.<\/p>\n\n\n\n<p>Here is a small comparative analysis of both approaches:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Approach<\/strong><\/td><td><strong>Time Complexity<\/strong><\/td><td><strong>Details<\/strong><\/td><\/tr><tr><td>Brute Force<\/td><td>O(n)<\/td><td>First count the length of the linked list and delete the nth node from last, i.e., length &#8211; nth node from starting.<\/td><\/tr><tr><td>Two Pointes<\/td><td>O(n)<\/td><td>Create a dummy node, and use two pointers, fast and slow to traverse the list and delete the nth node from last.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In conclusion, the article provides an overview of the &#8220;Remove Nth Node from End of List&#8221;  <a href=\"https:\/\/leetcode.com\/problems\/remove-nth-node-from-end-of-list\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">leetcode problem<\/a> in the context of a <a href=\"https:\/\/favtutor.com\/blog-details\/linked-list-data-structure\" data-type=\"link\" data-id=\"https:\/\/favtutor.com\/blog-details\/linked-list-data-structure\">linked list<\/a>. You can now easily implement both a naive brute-force approach and an optimized two-pointer approach. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Find out how to remove the nth node from the end of a linked list using two pointers approach, with implementation in C++, Java and Python.<\/p>\n","protected":false},"author":12,"featured_media":786,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[7],"tags":[14,15],"class_list":["post-783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-structures-algorithms","tag-dsa","tag-leetcode"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=783"}],"version-history":[{"count":6,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/783\/revisions"}],"predecessor-version":[{"id":870,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/783\/revisions\/870"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/786"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}