Nth Node from End in Linked List in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

struct Node {
    int val;
    struct Node* next;
};

struct Node* nthFromEnd(struct Node* head, int n) {
    struct Node *first = head, *second = head;
    for (int i = 0; i < n; i++)
        if (first) first = first->next;
        else return NULL;
    while (first) {
        first = first->next;
        second = second->next;
    }
    return second;
}

C Output

Input: 1→2→3→4→5, n = 2  
Output: 4


C++ Program

struct Node {
    int val;
    Node* next;
    Node(int x) : val(x), next(nullptr) {}
};

Node* nthFromEnd(Node* head, int n) {
    Node *first = head, *second = head;
    for (int i = 0; i < n; i++)
        if (first) first = first->next;
        else return NULL;
    while (first) {
        first = first->next;
        second = second->next;
    }
    return second;
}

C++ Output

Input: 10→20→30→40→50, n = 3  
Output: 30


JAVA Program

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

Node nthFromEnd(Node head, int n) {
    Node first = head, second = head;
    for (int i = 0; i < n; i++)
        if (first != null) first = first.next;
        else return null;
    while (first != null) {
        first = first.next;
        second = second.next;
    }
    return second;
}

JAVA Output

Input: 1→2→3→4→5, n = 1  
Output: 5


Python Program

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

def nth_from_end(head, n):
    first = second = head
    for _ in range(n):
        if not first: return None
        first = first.next
    while first:
        first = first.next
        second = second.next
    return second

Python Output

Input: 1→2→3→4→5, n = 3  
Output: 3


In-Depth Explanation
Example
Consider this list: 1 → 2 → 3 → 4 → 5. You are to go back the 2nd node from the end. The response is 4.

Rather than count the entire length and then traveling again, we employ two pointers:

Move the first pointer n steps forward

Then move first and second together until first reaches the end

At which time, second is at the Nth node from the end

This approach achieves the result in a single pass.

Real-Life Analogy
Imagine a race track with two runners:

One begins running initially and has a head start of n meters

The other begins from the start

When they both run at the same pace after starting, when one reaches the end, the other will be n meters away from the end

This analogy exactly describes the action of two pointers in this problem.

Why It Matters
This is a quintessential application of the two-pointer technique, which comes in handy when you are working with linked lists or arrays when relative distances or gaps between elements need to be computed. It enables you to execute operations in O(n) time using O(1) space, hence very efficient.

This method eliminates length-counting overhead, conserving time and memory — extremely crucial in memory-limited systems.

What You Learn from This
You get to know how to use two pointers in an intentional manner in order to keep a constant gap between them and advance them together. It is a beautiful method of processing arrays without having to store anything extra or perform traversals.

This question enhances your skills in traversing linked lists, null checks, and dynamic positioning in data structures.

Interview Relevance and Real Projects
This is a high-frequency interview question and is usually followed by more challenging problems such as:

Deleting the Nth node from the end

Printing all nodes from the Nth till the end

Reversing from the Nth

In practical applications, this is applicable in:

Task scheduling where you want to drop or emphasize new entries

Stream processing where the most recent n items are relevant

Data compression by truncating old nodes from the end

SEO-Optimized Explanation
Locating the Nth node from the end of a linked list is a fundamental algorithm that appears in numerous interview settings and system-level operations. By employing the two-pointer technique, programmers can get the target node in a single traversal, making it extremely efficient with O(n) time and O(1) space complexity. The method is popularly applied in memory-constrained data streaming, linked list manipulation, and competitive programming. Mastery of how to access the Nth node from the end in C, C++, Java, and Python improves your understanding of pointer-based reasoning and positions you for resolving practical data structure issues in software coding and coding interviews.