Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program for n'th node from the end of a Linked List in C program
In this tutorial, we will learn how to find the nth node from the end of a linked list in C. The program should print the nth node from the last without changing the order of nodes in the linked list.
Syntax
void findNthFromEnd(struct node* head, int n);
Example Input/Output
Input − 10 20 30 40 50 60 N = 3 Output − 40
In the above example, the third node from the end is 40. We can visualize this as −
Algorithm
The efficient approach to solve this problem −
- Count total nodes in the linked list
- Calculate position from start: position = count − n
- Traverse to that position and return the node
Method 1: Single Pass with Node Count
This method first counts the total nodes, then traverses to the required position −
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
struct node* head = NULL;
int count = 0;
void insert(int val) {
struct node* newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = val;
newnode->next = NULL;
if (head == NULL) {
head = newnode;
} else {
struct node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newnode;
}
count++;
}
void display() {
if (head == NULL) {
printf("List is empty<br>");
return;
}
struct node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("<br>");
}
void findNthFromEnd(int n) {
if (n > count || n <= 0) {
printf("Invalid position<br>");
return;
}
struct node* temp = head;
int position = count - n;
for (int i = 0; i < position; i++) {
temp = temp->next;
}
printf("%d node from the end is: %d<br>", n, temp->data);
}
int main() {
insert(10);
insert(20);
insert(30);
insert(40);
insert(50);
insert(60);
printf("Linked list: ");
display();
findNthFromEnd(3);
findNthFromEnd(1);
findNthFromEnd(6);
return 0;
}
Linked list: 10 20 30 40 50 60 3 node from the end is: 40 1 node from the end is: 60 6 node from the end is: 10
Method 2: Two-Pointer Technique
This method uses two pointers separated by n positions, eliminating the need to count nodes −
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
struct node* createNode(int data) {
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insert(struct node** head, int data) {
struct node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
int findNthFromEndTwoPointer(struct node* head, int n) {
struct node* first = head;
struct node* second = head;
// Move first pointer n positions ahead
for (int i = 0; i < n; i++) {
if (first == NULL) {
printf("List has fewer than %d nodes<br>", n);
return -1;
}
first = first->next;
}
// Move both pointers until first reaches end
while (first != NULL) {
first = first->next;
second = second->next;
}
return second->data;
}
void display(struct node* head) {
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("<br>");
}
int main() {
struct node* head = NULL;
insert(&head, 10);
insert(&head, 20);
insert(&head, 30);
insert(&head, 40);
insert(&head, 50);
printf("Linked list: ");
display(head);
int result = findNthFromEndTwoPointer(head, 2);
if (result != -1) {
printf("2nd node from end: %d<br>", result);
}
result = findNthFromEndTwoPointer(head, 5);
if (result != -1) {
printf("5th node from end: %d<br>", result);
}
return 0;
}
Linked list: 10 20 30 40 50 2nd node from end: 40 5th node from end: 10
Comparison
| Method | Time Complexity | Space Complexity | Passes |
|---|---|---|---|
| Count then Traverse | O(n) | O(1) | Two |
| Two Pointer | O(n) | O(1) | One |
Key Points
- Always validate that
nis within valid range (1 to list length) - The two-pointer technique is more efficient as it requires only one pass
- Both methods use constant extra space
Conclusion
Finding the nth node from the end can be efficiently solved using either counting approach or two-pointer technique. The two-pointer method is preferred as it requires only one traversal of the list.
