n नोड्स दिए जाने पर, लिंक्ड लिस्ट के अंत से nवें नोड को प्रिंट करना है। प्रोग्राम को लिस्ट में नोड्स का क्रम नहीं बदलना चाहिए, बल्कि केवल लिंक्ड लिस्ट के अंत से nवें नोड को प्रिंट करना चाहिए।
उदाहरण
इनपुट: 10 20 30 40 50 60, N=3 आउटपुट: 40
उपरोक्त उदाहरण में, पहले नोड से शुरू करके count-n नोड्स तक ट्रैवर्स किया जाता है, जैसे 10, 20, 30, 40, 50, 60 और इसलिए अंत से तीसरा नोड 40 है।

पूरी लिस्ट को ट्रैवर्स करने के बजाय यह कुशल दृष्टिकोण अपनाया जा सकता है:
- एक अस्थायी पॉइंटर लें, मान लें temp, नोड टाइप का
- इस temp पॉइंटर को पहले नोड पर सेट करें जिसे हेड पॉइंटर इंगित करता है
- काउंटर को लिस्ट में नोड्स की संख्या पर सेट करें
- temp को temp → next पर तब तक ले जाएँ जब तक count-n न हो जाए
- temp → data प्रदर्शित करें
यदि हम इस दृष्टिकोण का उपयोग करते हैं, तो काउंट 5 होगा और प्रोग्राम लूप को 5-3 यानी 2 तक इटरेट करेगा, इसलिए 10 से शुरू करके 0वें स्थान पर, फिर 20 पहले स्थान पर और 30 दूसरे स्थान पर जो कि परिणाम है। इस दृष्टिकोण से पूरी लिस्ट को अंत तक ट्रैवर्स करने की आवश्यकता नहीं होती, जिससे स्पेस और मेमोरी की बचत होती है।
एल्गोरिदम
शुरुआत
चरण 1 → नोड की संरचना बनाएं और temp, next, head को संरचना नोड के पॉइंटर के रूप में
struct node
int data
struct node *next, *head, *temp
समाप्त
चरण 2 → लिस्ट में नोड डालने का फंक्शन घोषित करें
void insert(int val)
struct node* newnode = (struct node*)malloc(sizeof(struct node))
newnode->data = val
यदि head = NULL तो
head = newnode सेट करें
head->next = NULL सेट करें
समाप्त
अन्यथा
temp = head सेट करें
लूप जब तक temp->next != NULL
temp = temp->next सेट करें
समाप्त
newnode->next = NULL सेट करें
temp->next = newnode सेट करें
समाप्त
चरण 3 → लिस्ट प्रदर्शित करने का फंक्शन घोषित करें
void display()
यदि head = NULL तो
"no node" प्रिंट करें
समाप्त
अन्यथा
temp = head सेट करें
लूप जब तक temp != NULL
temp->data प्रिंट करें
temp = temp->next सेट करें
समाप्त
समाप्त
चरण 4 → लिंक्ड लिस्ट के अंत से nवां नोड ढूंढने का फंक्शन घोषित करें
void last(int n)
int product=1, i घोषित करें
temp = head सेट करें
लूप i=0 से inext सेट करें
समाप्त
temp->data प्रिंट करें
चरण 5 → main() में
struct node* head = NULL का उपयोग करके नोड्स बनाएं
n को 3 के रूप में घोषित करें
नोड डालने के लिए insert(10) कॉल करें
लिस्ट प्रदर्शित करने के लिए display() कॉल करें
लिस्ट के अंत से nवां एलिमेंट ढूंढने के लिए last(n) कॉल करें
समाप्त
उदाहरण
#include <stdio.h>
#include <stdlib.h>
// नोड की संरचना
struct node{
int data;
struct node *next;
}*head,*temp;
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;
temp = head;
count++;
} else {
temp->next = newnode;
temp = temp->next;
count++;
}
}
// लिस्ट प्रदर्शित करने का फंक्शन
void display(){
if(head == NULL)
printf("no node ");
else {
temp = head;
while(temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
}
}
// लिंक्ड लिस्ट के अंत से 3रा नोड ढूंढने का फंक्शन
void last(int n){
int i;
temp = head;
for(i = 0; i < count - n; i++){
temp = temp->next;
}
printf("\n%drd node from the end of linked list is : %d", n, temp->data);
}
int main(){
// लिस्ट बनाना
struct node* head = NULL;
int n = 3;
// लिस्ट में एलिमेंट्स डालना
insert(1);
insert(2);
insert(3);
insert(4);
insert(5);
insert(6);
// लिस्ट प्रदर्शित करना
printf("\nlinked list is : ");
display();
// लिस्ट के अंत से nवां एलिमेंट ढूंढने के लिए फंक्शन कॉल करना
last(n);
return 0;
}
आउटपुट
linked list is : 1 2 3 4 5 6 3rd node from the end of linked list is : 4