{"id":655,"date":"2025-07-15T13:34:02","date_gmt":"2025-07-15T08:04:02","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=655"},"modified":"2025-07-15T13:34:04","modified_gmt":"2025-07-15T08:04:04","slug":"remove-nth-node-from-end-of-list-leetcode-19","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/remove-nth-node-from-end-of-list-leetcode-19\/","title":{"rendered":"Remove Nth Node From End of List | Leetcode 19"},"content":{"rendered":"\n<p>Given the&nbsp;<code>head<\/code>&nbsp;of a linked list, remove the&nbsp;<code>n<sup>th<\/sup><\/code>&nbsp;node from the end of the list and return its head.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/leetcode.com\/problems\/remove-nth-node-from-end-of-list\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/10\/03\/remove_ex1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> head = [1,2,3,4,5], n = 2<br><strong>Output:<\/strong> [1,2,3,5]<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> head = [1], n = 1<br><strong>Output:<\/strong> []<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> head = [1,2], n = 1<br><strong>Output:<\/strong> [1]<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The number of nodes in the list is&nbsp;<code>sz<\/code>.<\/li>\n\n\n\n<li><code>1 &lt;= sz &lt;= 30<\/code><\/li>\n\n\n\n<li><code>0 &lt;= Node.val &lt;= 100<\/code><\/li>\n\n\n\n<li><code>1 &lt;= n &lt;= sz<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Follow up:<\/strong>&nbsp;Could you do this in one pass?<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-3d14db5d-4e9d-474a-ab44-19bcc229ba18\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"true\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-nth-node-from-end-of-list-leetcode-19\/#0-solution-1-brute-force-approach-two-pass\" style=\"\">Solution 1: Brute Force Approach (Two Pass)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-nth-node-from-end-of-list-leetcode-19\/#1-intuition\" style=\"\">Intuition<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-nth-node-from-end-of-list-leetcode-19\/#2-detailed-approach\" style=\"\">Detailed Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-nth-node-from-end-of-list-leetcode-19\/#3-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-nth-node-from-end-of-list-leetcode-19\/#4-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-nth-node-from-end-of-list-leetcode-19\/#5-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-nth-node-from-end-of-list-leetcode-19\/#6-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-nth-node-from-end-of-list-leetcode-19\/#7-simplifying-it\" style=\"\">Simplifying It<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-nth-node-from-end-of-list-leetcode-19\/#8-solution-2-optimal-approach-two-pointers-one-pass\" style=\"\">Solution 2: Optimal Approach (Two Pointers &#8211; One Pass)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-nth-node-from-end-of-list-leetcode-19\/#9-intuition\" style=\"\">Intuition<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-nth-node-from-end-of-list-leetcode-19\/#10-detailed-approach\" style=\"\">Detailed Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-nth-node-from-end-of-list-leetcode-19\/#11-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-nth-node-from-end-of-list-leetcode-19\/#12-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-nth-node-from-end-of-list-leetcode-19\/#13-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-nth-node-from-end-of-list-leetcode-19\/#14-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-nth-node-from-end-of-list-leetcode-19\/#15-simplifying-it\" style=\"\">Simplifying It<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-nth-node-from-end-of-list-leetcode-19\/#16-summary\" style=\"\">Summary<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-solution-1-brute-force-approach-two-pass\">Solution 1: Brute Force Approach (Two Pass)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-intuition\">Intuition<\/h3>\n\n\n\n<p>Think of this like counting people in a line from the back! If you want to remove the 3rd person from the end, you first need to count how many people are in the line total. Then you can figure out which person to remove by doing some math. If there are 10 people and you want the 3rd from the end, you need to go to the 7th person from the front and remove the person next to them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-detailed-approach\">Detailed Approach<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Calculate List Length<\/strong>: Walk through the entire list to count total nodes<\/li>\n\n\n\n<li><strong>Handle Edge Case<\/strong>: If n equals the length, remove the head node<\/li>\n\n\n\n<li><strong>Find Position<\/strong>: Calculate where to stop (length &#8211; n nodes from the start)<\/li>\n\n\n\n<li><strong>Navigate to Position<\/strong>: Move to the node just before the one we want to remove<\/li>\n\n\n\n<li><strong>Remove Node<\/strong>: Skip the target node by updating the next pointer<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-code\">Code<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -&gt; Optional[ListNode]:\n        if head is None:\n            return None  # Empty list, nothing to remove\n\n        length = 0\n        current_node = head\n\n        # First pass: Calculate the length of the linked list\n        while current_node is not None:\n            length += 1\n            current_node = current_node.next\n\n        # Special case: If the node to remove is the head of the list\n        if length == n:\n            new_head = head.next  # New head is the second node\n            head = None           # Clean up old head\n            return new_head\n\n        # Find the node just before the one we want to remove\n        position_to_stop = length - n  # Position of node before target\n        current_node = head\n        count = 1\n\n        # Second pass: Navigate to the position just before target\n        while count &lt; position_to_stop:\n            current_node = current_node.next\n            count += 1\n\n        # Remove the nth node from the end by skipping it\n        current_node.next = current_node.next.next\n        return head\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">removeNthFromEnd<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">head<\/span><span style=\"color: #D4D4D4\">: Optional[ListNode], <\/span><span style=\"color: #9CDCFE\">n<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">) -&gt; Optional[ListNode]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> head <\/span><span style=\"color: #569CD6\">is<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #6A9955\"># Empty list, nothing to remove<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        length = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        current_node = head<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># First pass: Calculate the length of the linked list<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> current_node <\/span><span style=\"color: #569CD6\">is<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">not<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            length += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            current_node = current_node.next<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Special case: If the node to remove is the head of the list<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> length == n:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            new_head = head.next  <\/span><span style=\"color: #6A9955\"># New head is the second node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            head = <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">           <\/span><span style=\"color: #6A9955\"># Clean up old head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> new_head<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Find the node just before the one we want to remove<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        position_to_stop = length - n  <\/span><span style=\"color: #6A9955\"># Position of node before target<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        current_node = head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        count = <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Second pass: Navigate to the position just before target<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> count &lt; position_to_stop:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            current_node = current_node.next<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            count += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Remove the nth node from the end by skipping it<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        current_node.next = current_node.next.next<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> head<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-code-explanation\">Code Explanation<\/h3>\n\n\n\n<p>This solution uses two passes through the linked list. In the first pass, we count the total number of nodes to find the length. Once we know the length, we can calculate exactly which node to remove by converting &#8220;nth from the end&#8221; to &#8220;position from the beginning.&#8221; In the second pass, we navigate to the node just before our target and remove the target node by updating the next pointer to skip it. We handle the special case where we need to remove the head node separately.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let&#8217;s trace through:&nbsp;<code>1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5<\/code>, remove 2nd from end (n=2)<\/p>\n\n\n\n<p><strong>First Pass (Calculate Length):<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Visit nodes 1, 2, 3, 4, 5<\/li>\n\n\n\n<li>length = 5<\/li>\n<\/ul>\n\n\n\n<p><strong>Check Special Case:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>length (5) \u2260 n (2), so not removing head<\/li>\n<\/ul>\n\n\n\n<p><strong>Find Position:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>position_to_stop = 5 &#8211; 2 = 3<\/li>\n\n\n\n<li>Need to go to position 3 (node 3)<\/li>\n<\/ul>\n\n\n\n<p><strong>Second Pass (Navigate):<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Start at node 1, count = 1<\/li>\n\n\n\n<li>Move to node 2, count = 2<\/li>\n\n\n\n<li>Move to node 3, count = 3<\/li>\n\n\n\n<li>Stop (count = position_to_stop)<\/li>\n<\/ul>\n\n\n\n<p><strong>Remove Node:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>current_node = node 3<\/li>\n\n\n\n<li>current_node.next = node 4<\/li>\n\n\n\n<li>Set current_node.next = node 4.next (which is node 5)<\/li>\n\n\n\n<li>Node 4 is removed!<\/li>\n<\/ul>\n\n\n\n<p><strong>Result:<\/strong>&nbsp;<code>1 -&gt; 2 -&gt; 3 -&gt; 5<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>&nbsp;O(2n) &#8211; We traverse the list twice, but it&#8217;s still linear<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>&nbsp;O(1) &#8211; We only use a few variables<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-simplifying-it\">Simplifying It<\/h3>\n\n\n\n<p>This approach is like counting all the people in a line first, then figuring out which person to remove by doing some math. It&#8217;s straightforward but requires two trips through the line &#8211; one to count, one to remove.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8-solution-2-optimal-approach-two-pointers-one-pass\">Solution 2: Optimal Approach (Two Pointers &#8211; One Pass)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-intuition\">Intuition<\/h3>\n\n\n\n<p>Imagine you have two friends walking in a line, and you want one friend to be exactly n steps behind the other. If the front friend reaches the end of the line, the back friend will be exactly n steps from the end! This is the key insight &#8211; we don&#8217;t need to count the whole line first.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-detailed-approach\">Detailed Approach<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Set Up Two Pointers<\/strong>: Both start at the head of the list<\/li>\n\n\n\n<li><strong>Create Distance<\/strong>: Move the fast pointer n steps ahead<\/li>\n\n\n\n<li><strong>Handle Edge Case<\/strong>: If fast pointer reaches the end, we&#8217;re removing the head<\/li>\n\n\n\n<li><strong>Move Together<\/strong>: Move both pointers until fast pointer reaches the last node<\/li>\n\n\n\n<li><strong>Remove Node<\/strong>: The slow pointer will be just before the target node<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-code\">Code<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -&gt; Optional[ListNode]:\n        fastp = head  # Fast pointer\n        slowp = head  # Slow pointer\n\n        # Move fast pointer n steps ahead\n        for _ in range(n):\n            fastp = fastp.next\n\n        # Special case: If fast pointer is None, remove head\n        if fastp is None:\n            return head.next\n\n        # Move both pointers until fast reaches the last node\n        while fastp.next is not None:\n            fastp = fastp.next      # Move fast pointer forward\n            slowp = slowp.next      # Move slow pointer forward\n\n        # Remove the nth node from the end\n        slowp.next = slowp.next.next\n        return head\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">removeNthFromEnd<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">head<\/span><span style=\"color: #D4D4D4\">: Optional[ListNode], <\/span><span style=\"color: #9CDCFE\">n<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">) -&gt; Optional[ListNode]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        fastp = head  <\/span><span style=\"color: #6A9955\"># Fast pointer<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        slowp = head  <\/span><span style=\"color: #6A9955\"># Slow pointer<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Move fast pointer n steps ahead<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> _ <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(n):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            fastp = fastp.next<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Special case: If fast pointer is None, remove head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> fastp <\/span><span style=\"color: #569CD6\">is<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> head.next<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Move both pointers until fast reaches the last node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> fastp.next <\/span><span style=\"color: #569CD6\">is<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">not<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            fastp = fastp.next      <\/span><span style=\"color: #6A9955\"># Move fast pointer forward<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            slowp = slowp.next      <\/span><span style=\"color: #6A9955\"># Move slow pointer forward<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Remove the nth node from the end<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        slowp.next = slowp.next.next<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> head<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-code-explanation\">Code Explanation<\/h3>\n\n\n\n<p>We use two pointers with a fixed distance between them. First, we move the fast pointer n steps ahead to create the desired gap. If the fast pointer becomes None, it means we need to remove the head node. Otherwise, we move both pointers together until the fast pointer reaches the last node. At this point, the slow pointer is positioned just before the node we want to remove. We then remove the target node by updating the slow pointer&#8217;s next reference.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let&#8217;s trace through:&nbsp;<code>1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5<\/code>, remove 2nd from end (n=2)<\/p>\n\n\n\n<p><strong>Initial Setup:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>fastp = 1, slowp = 1<\/li>\n<\/ul>\n\n\n\n<p><strong>Create Distance (n=2 steps):<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Step 1: fastp = 2<\/li>\n\n\n\n<li>Step 2: fastp = 3<\/li>\n\n\n\n<li>Now fastp is 2 steps ahead of slowp<\/li>\n<\/ul>\n\n\n\n<p><strong>Check Special Case:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>fastp = 3 (not None), so not removing head<\/li>\n<\/ul>\n\n\n\n<p><strong>Move Both Pointers:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Step 1: fastp = 4, slowp = 2<\/li>\n\n\n\n<li>Step 2: fastp = 5, slowp = 3<\/li>\n\n\n\n<li>Stop (fastp.next is None)<\/li>\n<\/ul>\n\n\n\n<p><strong>Remove Node:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>slowp = 3, slowp.next = 4<\/li>\n\n\n\n<li>Set slowp.next = 4.next (which is 5)<\/li>\n\n\n\n<li>Node 4 is removed!<\/li>\n<\/ul>\n\n\n\n<p><strong>Result:<\/strong>&nbsp;<code>1 -&gt; 2 -&gt; 3 -&gt; 5<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"14-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>&nbsp;O(n) &#8211; We traverse the list only once<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>&nbsp;O(1) &#8211; We only use two pointer variables<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"15-simplifying-it\">Simplifying It<\/h3>\n\n\n\n<p>This approach is like having two friends walk together with a fixed distance between them. When the front friend reaches the end, the back friend is automatically at the right position. Much more efficient since we only need to walk through the line once!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"16-summary\">Summary<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Approach<\/th><th>Time<\/th><th>Space<\/th><th>Difficulty<\/th><th>Best For<\/th><\/tr><\/thead><tbody><tr><td>Brute Force (Two Pass)<\/td><td>O(n)<\/td><td>O(1)<\/td><td>Easy<\/td><td>Learning the concept<\/td><\/tr><tr><td>Optimal (Two Pointers)<\/td><td>O(n)<\/td><td>O(1)<\/td><td>Medium<\/td><td>Production code<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The&nbsp;<strong>two pointers approach<\/strong>&nbsp;is generally preferred because it solves the problem in a single pass while using the same space complexity. However, the brute force approach is more intuitive and easier to understand when you&#8217;re first learning about linked list manipulation.<\/p>\n\n\n\n<p>Both solutions effectively solve the problem, but the optimal solution showcases the power of the two-pointer technique, a fundamental skill for many linked list problems. The key insight is that we can maintain a fixed distance between two pointers to solve positional problems without needing to know the total length!<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given the&nbsp;head&nbsp;of a linked list, remove the&nbsp;nth&nbsp;node from the end of the list and return its head. Here&#8217;s the [Problem Link] to begin with. Example 1: Input: head = [1,2,3,4,5], n = 2Output: [1,2,3,5] Example 2: Input: head = [1], n = 1Output: [] Example 3: Input: head = [1,2], n = 1Output: [1] Constraints:<\/p>\n","protected":false},"author":1,"featured_media":656,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[19,29],"class_list":{"0":"post-655","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-intermediate","9":"tag-medium","10":"tag-singly-linked-list"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/remove-Nth-node-from-end-of-list-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/655","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=655"}],"version-history":[{"count":2,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/655\/revisions"}],"predecessor-version":[{"id":658,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/655\/revisions\/658"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/656"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}