LogIn
I don't have account.
Sponsored -55%
Tarkan Top Brand

Tarkan Portable Folding Laptop Desk with Drawer, Cup & Tablet Holder (Black)

Foldable · Anti-slip strip · 60 × 40 cm · No assembly

★★★★★ 4.2 (7,877) 1K+ bought last month
₹899 M.R.P. ₹1,999Save ₹1,100
Buy Now
Adℹ

Remove N-th Node from the End of a Linked List | Java

HackFury
45 Views
Amazon Pay offer
Adℹ

Given the head of a singly linked list and an integer n, remove the n-th node from the end of the list and return the head of the resulting list.

Removing the N-th node from the end of a singly linked list is a classic interview/algorithm problem. It tests your understanding of pointers, edge-case handling (removing the head) and different trade-offs between time/space approaches.

Example 1

Input: head = [1,2,3,4,5], n = 2
  Output: [1,2,3,5]
  Explanation: The 2nd node from the end is 4, so remove it.

Example 2

Input: head = [1], n = 1
  Output: []
  Explanation: Removing the only node yields an empty list.

Example 3

Input: head = [1,2], n = 2
  Output: [2]
  Explanation: The 2nd from end is the head (1), so new head is 2.

Constraints (typical)

  • Number of nodes: 1 <= length(head) <= 10^5 (can vary by platform)

  • Node values: arbitrary (e.g., -1000 <= Node.val <= 1000)

  • 1 <= n <= length(head)

Approaches to Solve the Problem

We'll solve this problem step by step, starting with the basic brute-force method, then making it faster with improvements and finally reaching the best one-pass solution.

Approach 1: Brute Force (Two-pass)

Idea:

  • Traverse once to calculate the length L of the linked list.

  • Compute the index to remove → L - n.

  • Traverse again to that index and remove the node.

class ListNode {
    int val;
    ListNode next;
    ListNode(int val) { this.val = val; }
}

public class RemoveNthNode {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        int length = 0;
        ListNode curr = head;
        while (curr != null) {
            length++;
            curr = curr.next;
        }
        int index = length - n;
        if (index == 0) return head.next;
        curr = head;
        for (int i = 1; i < index; i++) {
            curr = curr.next;
        }
        curr.next = curr.next.next;
        return head;
    }
}

Complexity:

  • Time: O(L) (two passes)

  • Space: O(1)

Approach 2: Using a Stack

  • Traverse the list and push every node onto a stack.

  • Pop n times - the popped node is the target.

  • The node left on top of the stack (if any) is the one just before the target. Adjust its pointer to skip the target node.

  • If the stack is empty after popping, it means we removed the head node.

  • We'll also handle the edge case where n is greater than the list length.

import java.util.Stack;

class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}

public class RemoveNthNodeStack {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if (head == null) return null;
        Stack<ListNode> stack = new Stack<>();
        ListNode curr = head;
        while (curr != null) {
            stack.push(curr);
            curr = curr.next;
        }
        if (n > stack.size()) {
            return head; // invalid input : return original list
        }
        for (int i = 0; i < n; i++) {
            curr = stack.pop();
        }
        if (stack.isEmpty()) {
            return head.next;
        }
        ListNode prev = stack.peek();
        prev.next = prev.next.next;
        return head;
    }
}

Complexity:

  • Time Complexity: O(L) -> traverse once + pop at most n times.

  • Space Complexity: O(L) -> store all nodes in the stack.

Approach 3: Recursive

Instead of iterating with loops, we use recursion. The recursion naturally processes the list from end to start during backtracking. This makes it easy to count nodes from the end without explicitly knowing the list length.

Recursive DFS: Each recursive call goes deeper into the list until null.

Backtracking Phase: When recursion unwinds, we start counting nodes from the end (pos = dfs(next) + 1).

Delete at n-th Node: When the counter reaches n+1, we know the current node is just before the one we want to remove.

We update the link:

node.next = node.next.next;

Why Use a Dummy Node?

  • If the head itself needs removal (n == length), recursion would fail without a dummy.

  • The dummy node ensures we always have a reference before head, making deletion safe.

class ListNode {
    int val;
    ListNode next;
    ListNode(int val) { this.val = val; }
}

public class RemoveNthNodeRecursive {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if (head == null) return null;
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        dfs(dummy, n);
        return dummy.next;
    }
    private int dfs(ListNode node, int n) {
        if (node == null) return 0;
        int pos = dfs(node.next, n) + 1;
        if (pos == n + 1) {
            node.next = node.next.next;
        }
        return pos;
    }
}

Complexity Analysis

  • Time Complexity: O(L), where L = length of the list (each node visited once).

  • Space Complexity: O(L) due to recursion stack (not as memory efficient as iterative approaches).

Approach 4: Two-Pointer (Optimal One-Pass)

We want to remove the n-th node from the end of a linked list. Instead of counting length first, we use two pointers (fast and slow):

Dummy Node

  • Create a dummy node before the head.
  • This makes edge cases (like removing the head) simpler.

Move fast n+1 steps ahead

  • Keep slow at dummy.
  • Now the gap between fast and slow is n+1.

Move both pointers together

  • Until fast reaches the end (null).
  • At this point, slow will be at the node just before the target node.

Delete the node

  • By skipping it: slow.next = slow.next.next;

Why n+1 Steps?

We want slow to stop at the node before the one we're deleting. So the gap must be n+1 steps (not just n).

Edge Cases

  • List is empty : return null.

  • n equals list length : remove the head.

  • n is larger than list length : either return original list or throw exception (depends on requirement).

class ListNode {
    int val;
    ListNode next;
    ListNode(int val) { this.val = val; }
}

public class RemoveNthNodeTwoPointer {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if (head == null) return null;
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode fast = dummy;
        ListNode slow = dummy;
        for (int i = 0; i <= n; i++) {
            if (fast == null) {
                return head;
            }
            fast = fast.next;
        }
        while (fast != null) {
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return dummy.next;
    }
}

Complexity Analysis

  • Time Complexity: O(L), where L = length of list (single pass).

  • Space Complexity: O(1), since we only use two pointers.

Complexity Comparison Table

Approach Time Complexity Space Complexity One-pass? Notes
Brute Force (2-pass) O(L) O(1) No Easy to explain, but not optimal
Stack O(L) O(L) No Uses extra memory
Two-pointer (Optimal) O(L) O(1) Yes Best choice for interviews
Recursive O(L) O(L) (stack) No Elegant, but risk of stack overflow
Responses (0)

Write a response

CommentHide Comments

No Comments yet.

"},"4":{"id":4,"name":"ZEBRONICS Blanc mouse ad","otherData":"{}","contentFormat":2,"content":"
Sponsored -53%
Zebronics #1 Best Seller

ZEBRONICS Blanc Slim Wireless Mouse — Rechargeable, BT + 2.4GHz (Black)

Up to 1600 DPI · Silent clicks · 63g · Multicolor LED

★★★★★ 4.0 (9,512) 5K+ bought last month
₹376 M.R.P. ₹799Save ₹423
Buy Now
"},"2":{"id":2,"name":"Crousal Ads","otherData":"{}","contentFormat":2,"content":"
Sponsored -34%
iQOO Amazon's Choice

iQOO Z10 Lite 5G — Cyber Green, 4GB RAM, 128GB Storage

Dimensity 6300 · 50MP Sony AI Camera · 6000 mAh · IP64

★★★★★ 4.0 (2,052) 500+ bought last month
₹18,997 M.R.P. ₹28,999Save ₹10,002
Buy Now
"},"1":{"id":1,"name":"Banner Ads","otherData":"{}","contentFormat":2,"content":"\n
\n \n \n \"Amazon\n \n
"}}},"blogDetails":{"isWriter":false,"likes":0,"isLikedByUser":false,"tagIds":[18,19,133,169,227],"tags":[{"id":18,"name":"Linked List","maskingName":"linked-list","isFeatured":true},{"id":19,"name":"Stack","maskingName":"stack","isFeatured":true},{"id":133,"name":"Coding Problems","maskingName":"coding-problems","isFeatured":true},{"id":169,"name":"Two Pointes","maskingName":"two-pointers","isFeatured":true},{"id":227,"name":"Sliding Window","maskingName":"sliding-window","isFeatured":true}],"comments":[],"bookmark":{"isBookmarked":false,"count":0},"author":{"userId":"MjlfNF84LVs0XTJuLXRDX0FOZ2VfX0l0","name":"HackFury"},"viewCount":44,"showBannerImage":false,"seoTags":null,"content":"

Given the head of a singly linked list and an integer n, remove the n-th node from the end of the list and return the head of the resulting list.

\n

Removing the N-th node from the end of a singly linked list is a classic interview/algorithm problem. It tests your understanding of pointers, edge-case handling (removing the head) and different trade-offs between time/space approaches.

\n

Example 1

\n
Input: head = [1,2,3,4,5], n = 2\n  Output: [1,2,3,5]\n  Explanation: The 2nd node from the end is 4, so remove it.\n
\n

Example 2

\n
Input: head = [1], n = 1\n  Output: []\n  Explanation: Removing the only node yields an empty list.\n
\n

Example 3

\n
Input: head = [1,2], n = 2\n  Output: [2]\n  Explanation: The 2nd from end is the head (1), so new head is 2.\n
\n

Constraints (typical)

\n\n

Approaches to Solve the Problem

\n

We'll solve this problem step by step, starting with the basic brute-force method, then making it faster with improvements and finally reaching the best one-pass solution.

\n

Approach 1: Brute Force (Two-pass)

\n

Idea:

\n\n
class ListNode {\n    int val;\n    ListNode next;\n    ListNode(int val) { this.val = val; }\n}\n\npublic class RemoveNthNode {\n    public ListNode removeNthFromEnd(ListNode head, int n) {\n        int length = 0;\n        ListNode curr = head;\n        while (curr != null) {\n            length++;\n            curr = curr.next;\n        }\n        int index = length - n;\n        if (index == 0) return head.next;\n        curr = head;\n        for (int i = 1; i < index; i++) {\n            curr = curr.next;\n        }\n        curr.next = curr.next.next;\n        return head;\n    }\n}\n
\n

Complexity:

\n\n

Approach 2: Using a Stack

\n\n
import java.util.Stack;\n\nclass ListNode {\n    int val;\n    ListNode next;\n    ListNode(int x) { val = x; }\n}\n\npublic class RemoveNthNodeStack {\n    public ListNode removeNthFromEnd(ListNode head, int n) {\n        if (head == null) return null;\n        Stack<ListNode> stack = new Stack<>();\n        ListNode curr = head;\n        while (curr != null) {\n            stack.push(curr);\n            curr = curr.next;\n        }\n        if (n > stack.size()) {\n            return head; // invalid input : return original list\n        }\n        for (int i = 0; i < n; i++) {\n            curr = stack.pop();\n        }\n        if (stack.isEmpty()) {\n            return head.next;\n        }\n        ListNode prev = stack.peek();\n        prev.next = prev.next.next;\n        return head;\n    }\n}\n
\n

Complexity:

\n\n

Approach 3: Recursive

\n

Instead of iterating with loops, we use recursion. The recursion naturally processes the list from end to start during backtracking. This makes it easy to count nodes from the end without explicitly knowing the list length.

\n

Recursive DFS: Each recursive call goes deeper into the list until null.

\n

Backtracking Phase: When recursion unwinds, we start counting nodes from the end (pos = dfs(next) + 1).

\n

Delete at n-th Node: When the counter reaches n+1, we know the current node is just before the one we want to remove.

\n

We update the link:

\n
node.next = node.next.next;\n
\n

Why Use a Dummy Node?

\n\n
class ListNode {\n    int val;\n    ListNode next;\n    ListNode(int val) { this.val = val; }\n}\n\npublic class RemoveNthNodeRecursive {\n    public ListNode removeNthFromEnd(ListNode head, int n) {\n        if (head == null) return null;\n        ListNode dummy = new ListNode(0);\n        dummy.next = head;\n        dfs(dummy, n);\n        return dummy.next;\n    }\n    private int dfs(ListNode node, int n) {\n        if (node == null) return 0;\n        int pos = dfs(node.next, n) + 1;\n        if (pos == n + 1) {\n            node.next = node.next.next;\n        }\n        return pos;\n    }\n}\n
\n

Complexity Analysis

\n\n

Approach 4: Two-Pointer (Optimal One-Pass)

\n

We want to remove the n-th node from the end of a linked list. Instead of counting length first, we use two pointers (fast and slow):

\n

Dummy Node

\n\n

Move fast n+1 steps ahead

\n\n

Move both pointers together

\n\n

Delete the node

\n\n

Why n+1 Steps?

\n

We want slow to stop at the node before the one we're deleting. So the gap must be n+1 steps (not just n).

\n

Edge Cases

\n\n
class ListNode {\n    int val;\n    ListNode next;\n    ListNode(int val) { this.val = val; }\n}\n\npublic class RemoveNthNodeTwoPointer {\n    public ListNode removeNthFromEnd(ListNode head, int n) {\n        if (head == null) return null;\n        ListNode dummy = new ListNode(0);\n        dummy.next = head;\n        ListNode fast = dummy;\n        ListNode slow = dummy;\n        for (int i = 0; i <= n; i++) {\n            if (fast == null) {\n                return head;\n            }\n            fast = fast.next;\n        }\n        while (fast != null) {\n            fast = fast.next;\n            slow = slow.next;\n        }\n        slow.next = slow.next.next;\n        return dummy.next;\n    }\n}\n
\n

Complexity Analysis

\n\n

Complexity Comparison Table

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ApproachTime ComplexitySpace ComplexityOne-pass?Notes
Brute Force (2-pass)O(L)O(1)NoEasy to explain, but not optimal
StackO(L)O(L)NoUses extra memory
Two-pointer (Optimal)O(L)O(1)YesBest choice for interviews
RecursiveO(L)O(L) (stack)NoElegant, but risk of stack overflow
","categoryId":177,"subCategoryId":177,"contentFormat":5,"blogId":21,"userId":"MjlfNF84LVs0XTJuLXRDX0FOZ2VfX0l0","title":"Remove N-th Node from the End of a Linked List | Java","url":"remove-n-th-node-from-the-end-of-a-linked-list-java","bannerImage":"","seoDescription":"","generatedOn":"2025-10-03T17:04:10","updatedOn":"2025-10-03T17:04:10"},"popularContents":[{"id":34,"title":"System Design Deep Dive: 25 Essential Interview Questions","url":"system-design-deep-dive-25-essential-interview-questions","description":null,"seoDescription":"","contentType":5,"generatedOn":"2026-10-14T23:20:09.9591636Z","viewCount":0},{"id":165,"title":"Top 50+ SQL Interview Questions and Answers for Intermediate to Advanced","url":"top-sql-interview-questions-and-answers-for-intermediate-to-advanced","description":null,"seoDescription":"Top SQL Interview Questions and Answers for Intermediate to Advanced. Top SQL Interview Questions for experience. Frequently asked SQL interview questions with answers","contentType":2,"generatedOn":"2026-10-14T23:20:09.9585586Z","viewCount":0},{"id":167,"title":"Meesho SDE-1 Online Assessment Interview Experience (Remote) - Rejected","url":"meesho-sde-1-online-assessment-interview-experience-remote-rejected","description":null,"seoDescription":"Read a real Meesho SDE-1 online assessment experience with coding questions, DSA topics, solutions, difficulty level, preparation tips and interview insights.","contentType":5,"generatedOn":"2026-10-14T23:20:09.9591702Z","viewCount":0},{"id":173,"title":"Uber SDE-2 Interview Experience : LLD, HLD, DSA & Hiring Manager Round","url":"uber-sde-2-interview-experience-lld-hld-dsa-hiring-manager-round","description":null,"seoDescription":"Read a real Uber SDE-2 interview experience covering DSA, low-level design, high-level system design, behavioral rounds, hiring freeze and key takeaways.","contentType":5,"generatedOn":"2026-10-14T23:20:09.9591809Z","viewCount":0},{"id":118,"title":"JioHotstar Staff Software Engineer Interview Experience (Bangalore)","url":"jiohotstar-staff-software-engineer-interview-experience-bangalore","description":null,"seoDescription":"JioHotstar Staff Software Engineer interview experience covering Uber system design, API rate limiter LLD, leadership rounds, bar raiser and HR insights.","contentType":5,"generatedOn":"2026-10-14T23:20:09.9591684Z","viewCount":0},{"id":172,"title":"Google L4 Software Engineer III interview experience | Selected","url":"google-l4-software-engineer-iii-interview-experience-selected","description":null,"seoDescription":"Google L4 Software Engineer III interview experience with API design, Googlyness, route matching, graph coding rounds, follow-up questions and tips.","contentType":5,"generatedOn":"2026-10-14T23:20:09.9591791Z","viewCount":0},{"id":168,"title":"Amazon SDE-2 Interview Experience (3 Years Experience)","url":"amazon-sde-2-interview-experience-3-years-experience","description":null,"seoDescription":" Read a real Amazon SDE-2 interview experience covering the online assessment, coding rounds, object-oriented design, leadership principles and system design.","contentType":5,"generatedOn":"2026-10-14T23:20:09.9591718Z","viewCount":0},{"id":35,"title":"Cursor vs Copilot : Which AI Coding Assistant Wins in 2025?","url":"cursor-vs-copilot-which-ai-coding-assistant-truly-wins-in-2025","description":null,"seoDescription":"Compare Cursor vs GitHub Copilot on speed, context awareness, multi-file support, pricing and code quality to choose the best AI coding assistant for developers","contentType":5,"generatedOn":"2026-10-14T23:20:09.9591667Z","viewCount":0},{"id":197,"title":"My Adobe SDE Interview Experience (Aug 2026) 5 Rounds, Selected","url":"my-adobe-sde-interview-experience-aug-2026-5-rounds-selected","description":null,"seoDescription":"Real Adobe SDE interview: 5 rounds, 4 DSA problems (Rotate Array, BSTs, Kth Smallest Matrix), code review & behavioral round. Full solutions + tips.","contentType":5,"generatedOn":"2026-10-14T23:20:09.9591825Z","viewCount":0},{"id":171,"title":"Meesho SDE-1 Backend Interview Experience (Remote | 3 Round) - Rejected","url":"meesho-sde-1-backend-interview-experience-remote-3-round-rejected","description":null,"seoDescription":"Read a real Meesho SDE-1 Backend interview experience covering the online assessment, DSA interview, machine coding round, LLD questions and preparation tips.","contentType":5,"generatedOn":"2026-10-14T23:20:09.9591739Z","viewCount":0}],"latestContents":[{"id":219,"title":"6 Tools That Made My Life Easier as a Software Engineer","url":"6-tools-that-made-my-life-easier-as-a-software-engineer","description":null,"seoDescription":"The 6 developer tools I use daily as a software engineer: Git, VS Code, Docker, Postman, Chrome DevTools, and AI assistants, with real tips, mistakes, and trade","contentType":5,"generatedOn":"2026-10-14T23:20:09.9590841Z","viewCount":0},{"id":218,"title":"When Should a Business Use Multiple AI Agents Instead of One?","url":"when-should-a-business-use-multiple-ai-agents-instead-of-one","description":null,"seoDescription":"Single agent or multi-agent AI? Learn the 4 signals that justify splitting agents by knowledge, permissions, and risk, plus routing and handoff tips.","contentType":5,"generatedOn":"2026-10-14T23:20:09.9590887Z","viewCount":0},{"id":216,"title":"AI in Clinical Trials Market Estimated to Experience a Hike in Growth by 2035","url":"ai-in-clinical-trials-market-estimated-to-experience-a-hike-in-growth-by-2035","description":null,"seoDescription":"The exclusive information about market dynamics serves as a valuable guide to predict economic scenarios and initiatives taken to enhance future growth. Our mar","contentType":5,"generatedOn":"2026-10-14T23:20:09.9590903Z","viewCount":0},{"id":217,"title":"AI in Clinical Trials Market Estimated to Experience a Hike in Growth by 2035","url":"ai-in-clinical-trials-market-estimated-to-experience-a-hike-in-growth-by-2035","description":null,"seoDescription":"The exclusive information about market dynamics serves as a valuable guide to predict economic scenarios and initiatives taken to enhance future growth. Our mar","contentType":5,"generatedOn":"2026-10-14T23:20:09.9590918Z","viewCount":0},{"id":215,"title":"What Happens When an AI Agent Doesn't Know the Answer?","url":"what-happens-when-an-ai-agent-doesnt-know-the-answer","description":null,"seoDescription":"Most AI agents fail quietly. They guess instead of saying \"I don't know.\" Here's how to design better fallback and human-handoff behavior.","contentType":5,"generatedOn":"2026-10-14T23:20:09.9590931Z","viewCount":0},{"id":214,"title":"Greedy Algorithms Explained: How They Work, When They Fail and How to Use Them","url":"greedy-algorithms-explained-how-they-work-when-they-fail-and-how-to-use-them","description":null,"seoDescription":"Learn greedy algorithms step by step with C# code Dijkstra, Huffman coding, knapsack and MSTs plus when greedy fails and how to prove it's correct","contentType":5,"generatedOn":"2026-10-14T23:20:09.9590948Z","viewCount":0},{"id":213,"title":"Amazon SDE II Interview Experience (Bangalore, Selected) ~ Aug 2026","url":"amazon-sde-ii-interview-experience-bangalore-selected-aug-2026","description":null,"seoDescription":"Amazon SDE II interview experience from Bangalore covering 4 rounds, DSA, LLD, system design, Leadership Principles, coding questions and selection.","contentType":5,"generatedOn":"2026-10-14T23:20:09.9590963Z","viewCount":0},{"id":212,"title":"How to Get 10x Better AI Answers Without Writing Better Prompts : 10 Proven Techniques","url":"how-to-get-10x-better-ai-answers-without-writing-better-prompts-10-proven-techniques","description":null,"seoDescription":"Get better AI answers without complex prompts. Learn 10 practical techniques using context, examples, tools, feedback, and verification.","contentType":5,"generatedOn":"2026-10-14T23:20:09.9590977Z","viewCount":0},{"id":211,"title":"I Interviewed for a Microsoft SDE I Role. Here's Everything That Happened.","url":"i-interviewed-for-a-microsoft-sde-i-role-heres-everything-that-happened","description":null,"seoDescription":"My real Microsoft SDE I interview experience, all 4 rounds, the exact DSA problems with fully tested solutions, the WhatsApp system design round","contentType":5,"generatedOn":"2026-10-14T23:20:09.9591021Z","viewCount":0},{"id":140,"title":"PolicyBazaar Software Development Engineer (SDE) Interview Experience | DSA, Core CS & System Design","url":"policybazaar-software-development-engineer-sde-interview-experience-dsa-core-cs-system-design","description":null,"seoDescription":"Detailed PolicyBazaar SDE interview experience covering 3 rounds: DSA coding, core CS assessment and system design with key learnings and insights.","contentType":5,"generatedOn":"2026-10-14T23:20:09.9591035Z","viewCount":0}],"relatedContents":[{"id":207,"title":"How to Find the Right View of a Binary Tree","url":"right-view-of-a-binary-tree","description":null,"seoDescription":"Learn how to find the right view of a binary tree using DFS and BFS, with examples, code, complexity analysis and coding interview tips.","contentType":5,"generatedOn":"2026-10-14T23:20:09.9638275Z","viewCount":0},{"id":206,"title":"Left View of a Binary Tree: DFS and BFS Approaches Explained","url":"left-view-of-a-binary-tree","description":null,"seoDescription":"Learn how to find the left view of a binary tree using DFS and BFS, with examples, code, complexity analysis, common mistakes and interview tips.","contentType":5,"generatedOn":"2026-10-14T23:20:09.9638315Z","viewCount":0},{"id":205,"title":"Top View of a Binary Tree Using BFS and DFS | Complete Guide","url":"top-view-of-a-binary-tree","description":null,"seoDescription":"Learn how to find the top view of a binary tree using DFS and BFS with step-by-step explanations, examples, code, complexity analysis and interview insights.","contentType":5,"generatedOn":"2026-10-14T23:20:09.963833Z","viewCount":0},{"id":201,"title":"Bottom View of a Binary Tree: DFS vs BFS Approach","url":"bottom-view-of-a-binary-tree-dfs-vs-bfs-approach","description":null,"seoDescription":"Learn to find the bottom view of a binary tree using horizontal distance. Compares a buggy DFS attempt, a fixed DFS and the optimal BFS solution in Java.","contentType":5,"generatedOn":"2026-10-14T23:20:09.9638342Z","viewCount":0},{"id":200,"title":" Longest Playlist With No Repeated Songs : Brute Force to Optimal","url":"longest-playlist-with-no-repeated-songs-brute-force-to-optimal","description":null,"seoDescription":"Find the longest playlist with no repeated songs using brute force, HashSet, and HashMap sliding window techniques, with Java code and dry runs.","contentType":5,"generatedOn":"2026-10-14T23:20:09.9638353Z","viewCount":0},{"id":90,"title":"Contain Virus : Deep, Intuitive & Fully Explained Guide","url":"contain-virus-deep-intuitive-fully-explained-guide","description":null,"seoDescription":"Contain Virus problem explained with DFS + greedy approach, step-by-step simulation, examples and optimized solutions for coding interviews.","contentType":5,"generatedOn":"2026-10-14T23:20:09.9638368Z","viewCount":0},{"id":37,"title":"Find the repeating and missing numbers","url":"find-the-repeating-and-missing-numbers","description":null,"seoDescription":"Find the repeating and missing numbers in an array from 1..n using brute force, hashmap, math and XOR, step-by-step explained with real-world use cases.","contentType":5,"generatedOn":"2026-10-14T23:20:09.9638382Z","viewCount":0},{"id":27,"title":"4 Sum | Find Quads that add up to a target value","url":"4-sum-find-quads-that-add-up-to-a-target-value","description":null,"seoDescription":"","contentType":5,"generatedOn":"2026-10-14T23:20:09.9638395Z","viewCount":0},{"id":26,"title":"Top 50 Most Frequently Asked String Coding Problems (Interview Preparation )","url":"top-50-most-frequently-asked-string-coding-problems-interview-preparation","description":null,"seoDescription":"","contentType":5,"generatedOn":"2026-10-14T23:20:09.9638409Z","viewCount":0},{"id":25,"title":"Intersection of Two Sorted Arrays","url":"intersection-of-two-sorted-arrays","description":null,"seoDescription":"","contentType":5,"generatedOn":"2026-10-14T23:20:09.9638422Z","viewCount":0}]}}},"source":{"isMobile":false}}; window.__CLIENT_RENDER__ = false;