Remove the Nth Node From the End of a Linked List – Optimal Two-Pointer Approach (LeetCode 19)

Remove the Nth Node From the End of a Linked List

Meta Description: Master the efficient two-pointer method to remove the Nth node from the end of a linked list. Includes step-by-step Java code explanation and edge case handling for LeetCode Problem 19.


🧠 Problem Summary

You’re given the head of a singly linked list. Your task is to remove the Nth node from the end of the list and return the updated head. This is a classic linked list problem and frequently asked in coding interviews.

🔍 Example

Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
Explanation: The node with value 4 (2nd from the end) is removed.


✅ Why Use the Two-Pointer Technique?

A naive approach involves two passes:

  1. Count the total nodes.
  2. Delete the (length - n)th node.

But we can do better.

Using the two-pointer technique, we remove the target node in one pass, resulting in:

  • Time Complexity: O(N)
  • Space Complexity: O(1)

This is ideal for large linked lists and optimal performance.


🧩 Step-by-Step Algorithm

🔗 Core Steps:

  1. Initialize two pointers: slow and fast, both pointing to the head.
  2. Advance fast by n steps to create a gap of n nodes.
  3. Move both pointers simultaneously until fast reaches the end.
  4. Remove the target node by adjusting slow.next.

💻 Java Code Implementation

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if (head == null || head.next == null) return null;
        
        ListNode slow = head, fast = head;

        // Move fast pointer n steps ahead
        while (n-- > 0) {
            fast = fast.next;
        }

        // Edge case: removing the head node
        if (fast == null) {
            return head.next;
        }

        // Move both pointers until fast reaches the last node
        while (fast.next != null) {
            slow = slow.next;
            fast = fast.next;
        }

        // Skip the Nth node from the end
        slow.next = slow.next.next;

        return head;
    }
}

🧠 Code Walkthrough

🔍 1. Edge Case Handling

  • If the list has only one node, removing it returns null.
  • If n equals the length of the list, then fast becomes null after n steps — which means we need to remove the head.

🏃 2. Fast-Slow Traversal

  • The fast pointer moves n steps ahead, creating a buffer.
  • Once fast hits the end, slow is right before the node to remove.

🧹 3. Node Deletion

  • slow.next = slow.next.next effectively skips over the target node, deleting it from the list.

📈 Complexity Analysis

MetricValue
TimeO(N) – One full traversal
SpaceO(1) – No extra memory used

🔍 Visual Example

Consider: head = [1 → 2 → 3 → 4 → 5], n = 2

  1. After moving fast 2 steps:
    fast = node 3, slow = node 1
  2. Move both pointers until fast reaches the end:
    fast = node 5, slow = node 3
  3. Delete slow.next (node 4):
    Resulting list = [1 → 2 → 3 → 5]

🧠 Final Thoughts

The two-pointer method is a powerful and elegant technique for solving many linked list problems. For LeetCode 19, it lets us remove the Nth node from the end in a single pass, making it a go-to strategy in coding interviews.


🧪 Practice More Linked List Problems


Got any questions? Confused about edge cases? Drop your doubts below — let’s learn together! 🚀


Keywords: remove nth node from end of linked list, linked list deletion Java, two-pointer technique, LeetCode 19 Java solution, optimal linked list algorithm, coding interview prep, linked list Java code.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *