← Back to Codes

2026-06-09 16:36:42

Remove Nth Node From End of List in Python and Java | LeetCode #19

Learn how to solve Remove Nth Node From End of List in Python and Java with beginner-friendly explanation, step-by-step dry run, custom examples, and time complexity.

Remove Nth Node From End of List in Python and Java

In this tutorial, we will learn LeetCode #19: Remove Nth Node From End of List in very simple language. We will understand the idea step by step, see custom examples, and write complete code in Python and Java.

What Is the Remove Nth Node From End of List Problem?

This problem asks us to solve a common coding interview task using the given input. The goal is to return the correct result without using a slow brute force method.

For this problem, we will use two pointers because it gives a clean and optimized solution.

Example input:
head = [6, 1, 4, 9, 2], n = 2

Expected result:
Output: [6, 1, 4, 2]

Explanation:
Remove the 2nd node from the end, which is 9.

Beginner-Friendly Idea

The main idea is to avoid trying every possible answer blindly. Instead, we keep useful information while reading the input and use that information to make the next decision.

At each step, ask: “What do I already know, and how does the current value change my answer?”

Using our example:
head = [6, 1, 4, 9, 2], n = 2

Approach:
two pointers

We update variables step by step until we reach:
Output: [6, 1, 4, 2]

Why Do We Use This Approach?

A direct brute force solution is usually easier to think about, but it can become slow when the input is large. The optimized approach keeps only the important state and avoids repeated work.

That is why two pointers is useful for this problem.

Step-by-Step Explanation

Let us dry run the algorithm using a custom example.

Step 1
Use this custom example.

head = [6, 1, 4, 9, 2], n = 2

We will solve it using two pointers.

Step 2
Look at the first important value from the example and create the variables needed by the algorithm.

current_state = based on the first value
answer = not finished yet

Step 3
Move to the next useful value and update the state.

The algorithm compares the new value with the old state.
If the new value improves the answer, we update the answer.

Step 4
Continue this process until all useful values are processed.

After processing the example, we get:

Output: [6, 1, 4, 2]

Why?
Remove the 2nd node from the end, which is 9.

Important Code Logic

The most important part is updating the algorithm state after reading each useful value. This is where the answer becomes better step by step.

Think like this:

old_state = what we knew before
current_value = value we are checking now
new_state = updated result after using current_value

For our example, the final state gives:
Output: [6, 1, 4, 2]

Example 1

Input:
head = [6, 1, 4, 9, 2], n = 2

Output:
Output: [6, 1, 4, 2]

Explanation:
Remove the 2nd node from the end, which is 9.

Example 2

Input:
head = [1, 5, 9, 12], n = 1

Output:
[1, 5, 9]

Explanation:
The last node is removed.

Python Code

Here is the complete Python solution for LeetCode #19.

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class NthNodeRemover:
    def remove_nth_from_end(self, head, n):
        dummy = ListNode(0, head)
        fast = dummy
        slow = dummy

        for _ in range(n + 1):
            fast = fast.next

        while fast:
            fast = fast.next
            slow = slow.next

        slow.next = slow.next.next
        return dummy.next


def build_linked_list(values):
    dummy = ListNode()
    current = dummy
    for value in values:
        current.next = ListNode(value)
        current = current.next
    return dummy.next


def linked_list_to_list(head):
    values = []
    while head:
        values.append(head.val)
        head = head.next
    return values


remover = NthNodeRemover()
head = build_linked_list([4, 7, 9, 12, 15])
print(linked_list_to_list(remover.remove_nth_from_end(head, 2)))  # Output: [4, 7, 9, 15]

Java Code

Here is the complete Java solution for LeetCode #19.

import java.util.ArrayList;
import java.util.List;

class ListNode {
    int val;
    ListNode next;

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

class NthNodeRemover {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode fast = dummy;
        ListNode slow = dummy;

        for (int i = 0; i <= n; i++) {
            fast = fast.next;
        }

        while (fast != null) {
            fast = fast.next;
            slow = slow.next;
        }

        slow.next = slow.next.next;
        return dummy.next;
    }

    private static ListNode buildList(int[] values) {
        ListNode dummy = new ListNode(0);
        ListNode current = dummy;
        for (int value : values) {
            current.next = new ListNode(value);
            current = current.next;
        }
        return dummy.next;
    }

    private static List<Integer> toList(ListNode head) {
        List<Integer> values = new ArrayList<>();
        while (head != null) {
            values.add(head.val);
            head = head.next;
        }
        return values;
    }

    public static void main(String[] args) {
        NthNodeRemover remover = new NthNodeRemover();
        ListNode head = buildList(new int[] {4, 7, 9, 12, 15});
        System.out.println(toList(remover.removeNthFromEnd(head, 2))); // [4, 7, 9, 15]
    }
}

Time and Space Complexity

Time Complexity: O(n log n)

The time complexity depends on how many values the algorithm needs to process and whether it uses sorting, binary search, heap, or traversal.

Space Complexity: O(n)

The extra space is used for the variables or data structures needed by the optimized approach.

Final Summary

LeetCode #19: Remove Nth Node From End of List becomes easier when we break it into small steps. First understand what the problem asks, then track the important state, dry run with an example, and finally write the code.