{"id":6901,"date":"2025-06-17T20:22:13","date_gmt":"2025-06-17T14:52:13","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=6901"},"modified":"2025-06-17T20:41:21","modified_gmt":"2025-06-17T15:11:21","slug":"remove-nth-node-from-end-of-singly-linked-list-two-pass-approach","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/remove-nth-node-from-end-of-singly-linked-list-two-pass-approach\/","title":{"rendered":"Remove Nth Node From End of Singly Linked List (Two Pass Approach)"},"content":{"rendered":"\n<!-- PrismJS for Syntax Highlighting -->\n<link href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.min.css\" rel=\"stylesheet\">\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.min.js\"><\/script>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/plugins\/autoloader\/prism-autoloader.min.js\"><\/script>\n\n<style>\n  .wp_blog_main-heading {\n    text-align: center;\n    font-size: 2.4rem;\n    color: #E58C32;\n    margin-top: 2.5rem;\n    font-weight: bold;\n  }\n\n  .wp_blog_explanation,\n  .wp_blog_code-tabs-container {\n    max-width: 940px;\n    margin: 2rem auto;\n    padding: 2rem;\n    border-radius: 12px;\n    background-color: #f9fafb;\n  }\n\n  .wp_blog_explanation h2 {\n    font-size: 1.4rem;\n    color: #E58C32;\n    margin-bottom: 0.75rem;\n  }\n\n  .wp_blog_explanation p,\n  .wp_blog_explanation li {\n    font-size: 1.05rem;\n    line-height: 1.7;\n    margin: 0.5rem 0;\n    color: #1f2937;\n  }\n\n  .wp_blog_explanation code {\n    padding: 3px 6px;\n    border-radius: 4px;\n    background: #e5e7eb;\n    font-family: 'Courier New', monospace;\n  }\n\n  .wp_blog_code-tabs-header {\n    display: flex;\n    flex-wrap: wrap;\n    gap: 0.5rem;\n    margin-bottom: 1rem;\n  }\n\n  .wp_blog_code-tab-button {\n    padding: 0.6rem 1.2rem;\n    border: 1px solid #E58C32;\n    color: #E58C32;\n    border-radius: 50px;\n    font-weight: 600;\n    cursor: pointer;\n    background-color: white;\n    transition: background 0.3s ease, color 0.3s ease;\n  }\n\n  .wp_blog_code-tab-button.active {\n    background: #E58C32;\n    color: white;\n  }\n\n  .wp_blog_code-tab-content {\n    display: none;\n    background: #111827;\n    border-radius: 12px;\n  }\n\n  .wp_blog_code-tab-content.active {\n    display: block;\n  }\n\n  .wp_blog_code-tab-content pre {\n    margin: 0;\n    padding: 1.5rem;\n    font-size: 1rem;\n    overflow-x: auto;\n    color: #f3f4f6;\n    background: #111827;\n    border-radius: 12px;\n  }\n<\/style>\n\n<div class=\"wp_blog_explanation\">\n  <h2>Problem Statement:<\/h2>\n  <p>Given the head of a linked list, remove the <code>n<\/code>th node from the end of the list and return its head.<\/p>\n\n  <h2>Examples:<\/h2>\n  <ul>\n    <li><strong>Input:<\/strong> head = [1,2,3,4,5], n = 2<br><strong>Output:<\/strong> [1,2,3,5]<\/li>\n    <li><strong>Input:<\/strong> head = [1], n = 1<br><strong>Output:<\/strong> []<\/li>\n    <li><strong>Input:<\/strong> head = [1,2], n = 1<br><strong>Output:<\/strong> [1]<\/li>\n  <\/ul>\n\n  <h2>Constraints:<\/h2>\n  <ul>\n    <li>The number of nodes in the list is <code>sz<\/code>.<\/li>\n    <li>1 \u2264 sz \u2264 30<\/li>\n    <li>0 \u2264 Node.val \u2264 100<\/li>\n    <li>1 \u2264 n \u2264 sz<\/li>\n  <\/ul>\n\n  <h2>Approach:<\/h2>\n  <ul>\n    <li>Use a sentinel (dummy) node before the head to simplify edge cases.<\/li>\n    <li>First, calculate the total length of the list.<\/li>\n    <li>Find the previous node of the one to be deleted using the length &#8211; n formula.<\/li>\n    <li>Update the links to skip the target node.<\/li>\n  <\/ul>\n\n  <h2>Time and Space Complexity:<\/h2>\n  <ul>\n    <li><strong>Time Complexity:<\/strong> O(n)<\/li>\n    <li><strong>Space Complexity:<\/strong> O(1)<\/li>\n  <\/ul>\n<\/div>\n\n<div class=\"wp_blog_code-tabs-container\">\n  <div class=\"wp_blog_code-tabs-header\">\n    <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"cs\">C#<\/button>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n    <pre><code class=\"language-javascript\">\nvar removeNthFromEnd = function(head, n) {\n    let sentinel = new ListNode(0, head);\n    let length = 0;\n    let first = head;\n    while (first) {\n        length++;\n        first = first.next;\n    }\n    let prev = sentinel;\n    for (let i = 0; i < length - n; i++) {\n        prev = prev.next;\n    }\n    prev.next = prev.next.next;\n    return sentinel.next;\n};\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n    <pre><code class=\"language-python\">\nclass Solution(object):\n    def removeNthFromEnd(self, head, n):\n        sentinel = ListNode(0)\n        sentinel.next = head\n        length = 0\n        current = head\n        while current:\n            length += 1\n            current = current.next\n        prev = sentinel\n        for _ in range(length - n):\n            prev = prev.next\n        prev.next = prev.next.next\n        return sentinel.next\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n    <pre><code class=\"language-java\">\nclass Solution {\n    public ListNode removeNthFromEnd(ListNode head, int n) {\n        ListNode sentinel = new ListNode(0);\n        sentinel.next = head;\n        int length = 0;\n        ListNode current = head;\n        while (current != null) {\n            length++;\n            current = current.next;\n        }\n        ListNode prev = sentinel;\n        for (int i = 0; i < length - n; i++) {\n            prev = prev.next;\n        }\n        prev.next = prev.next.next;\n        return sentinel.next;\n    }\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n    <pre><code class=\"language-cpp\">\nclass Solution {\npublic:\n    ListNode* removeNthFromEnd(ListNode* head, int n) {\n        ListNode* sentinel = new ListNode(0);\n        sentinel->next = head;\n        int length = 0;\n        ListNode* current = head;\n        while (current) {\n            length++;\n            current = current->next;\n        }\n        ListNode* prev = sentinel;\n        for (int i = 0; i < length - n; i++) {\n            prev = prev->next;\n        }\n        prev->next = prev->next->next;\n        return sentinel->next;\n    }\n};\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n    <pre><code class=\"language-c\">\nstruct ListNode* removeNthFromEnd(struct ListNode* head, int n) {\n    struct ListNode dummy;\n    dummy.next = head;\n    int length = 0;\n    struct ListNode* current = head;\n    while (current) {\n        length++;\n        current = current->next;\n    }\n    struct ListNode* prev = &dummy;\n    for (int i = 0; i < length - n; i++) {\n        prev = prev->next;\n    }\n    prev->next = prev->next->next;\n    return dummy.next;\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"cs\">\n    <pre><code class=\"language-csharp\">\npublic class Solution {\n    public ListNode RemoveNthFromEnd(ListNode head, int n) {\n        ListNode sentinel = new ListNode(0, head);\n        int length = 0;\n        ListNode current = head;\n        while (current != null) {\n            length++;\n            current = current.next;\n        }\n        ListNode prev = sentinel;\n        for (int i = 0; i < length - n; i++) {\n            prev = prev.next;\n        }\n        prev.next = prev.next.next;\n        return sentinel.next;\n    }\n}\n    <\/code><\/pre>\n  <\/div>\n<\/div>\n\n<script>\n  document.addEventListener('DOMContentLoaded', () => {\n    const buttons = document.querySelectorAll('.wp_blog_code-tab-button');\n    const contents = document.querySelectorAll('.wp_blog_code-tab-content');\n    buttons.forEach(button => {\n      button.addEventListener('click', () => {\n        const lang = button.getAttribute('data-lang');\n        buttons.forEach(btn => btn.classList.remove('active'));\n        contents.forEach(content => content.classList.remove('active'));\n        button.classList.add('active');\n        document.querySelector(`.wp_blog_code-tab-content[data-lang=\"${lang}\"]`).classList.add('active');\n      });\n    });\n  });\n<\/script>\n\n","protected":false},"excerpt":{"rendered":"<p>Problem Statement: Given the head of a linked list, remove the nth node from the end of the list and return its head. Examples: Input: head = [1,2,3,4,5], n = 2Output: [1,2,3,5] Input: head = [1], n = 1Output: [] Input: head = [1,2], n = 1Output: [1] Constraints: The number of nodes in the<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[260,176,245,810,174,172,173],"tags":[],"class_list":{"0":"post-6901","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-c-c-plus-plus","7":"category-csharp","8":"category-data-science-and-machine-learning","9":"category-dsa","10":"category-java","11":"category-javascript","12":"category-python"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=6901"}],"version-history":[{"count":2,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6901\/revisions"}],"predecessor-version":[{"id":6906,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6901\/revisions\/6906"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=6901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=6901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=6901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}