0
0
CHow-ToBeginner · 3 min read

How to Find nth Node from End in C: Simple Approach

To find the nth node from the end in C, use two pointers: move the first pointer n steps ahead, then move both pointers together until the first reaches the end. The second pointer will then point to the nth node from the end.
📐

Syntax

This is the typical function signature and usage pattern to find the nth node from the end in a singly linked list:

  • struct Node*: Pointer to the linked list node.
  • int n: The position from the end to find.
  • The function returns a pointer to the nth node from the end or NULL if not found.
c
struct Node* findNthFromEnd(struct Node* head, int n);
💻

Example

This example demonstrates how to find the 3rd node from the end in a linked list using the two-pointer method.

c
#include <stdio.h>
#include <stdlib.h>

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

// Function to create a new node
struct Node* newNode(int data) {
    struct Node* node = (struct Node*)malloc(sizeof(struct Node));
    node->data = data;
    node->next = NULL;
    return node;
}

// Function to find nth node from end
struct Node* findNthFromEnd(struct Node* head, int n) {
    struct Node *first = head, *second = head;
    int count = 0;

    // Move first pointer n steps ahead
    while (count < n) {
        if (first == NULL) return NULL; // n is larger than list length
        first = first->next;
        count++;
    }

    // Move both pointers until first reaches end
    while (first != NULL) {
        first = first->next;
        second = second->next;
    }

    return second;
}

int main() {
    // Create linked list 10->20->30->40->50
    struct Node* head = newNode(10);
    head->next = newNode(20);
    head->next->next = newNode(30);
    head->next->next->next = newNode(40);
    head->next->next->next->next = newNode(50);

    int n = 3;
    struct Node* result = findNthFromEnd(head, n);

    if (result != NULL)
        printf("%dth node from end is %d\n", n, result->data);
    else
        printf("List is shorter than %d nodes\n", n);

    return 0;
}
Output
3th node from end is 30
⚠️

Common Pitfalls

Common mistakes when finding the nth node from the end include:

  • Not checking if n is larger than the list length, which causes null pointer errors.
  • Moving the pointers incorrectly, such as moving the second pointer before the first pointer is advanced n steps.
  • Not handling empty lists or single-node lists properly.
c
# Wrong approach: moving second pointer too early
struct Node* wrongFindNthFromEnd(struct Node* head, int n) {
    struct Node *first = head, *second = head;
    int count = 0;

    while (first != NULL) {
        if (count >= n) {
            second = second->next; // Moves second too early
        }
        first = first->next;
        count++;
    }

    if (count < n) return NULL;
    return second;
}

// Correct approach is to move first pointer n steps first, then move both pointers together.

Key Takeaways

Use two pointers: advance the first pointer n steps before moving both together.
Always check if n is larger than the list length to avoid errors.
Return NULL if the list is shorter than n nodes.
This method finds the nth node from the end in a single pass efficiently.