// { Driver Code Starts // C program to find n'th Node in linked list #include <stdio.h> #include <stdlib.h> #include<iostream> using namespace std; /* Link list Node */ struct Node { int data; struct Node *next; Node(int x) { data = x; next = NULL; } }; /* Function to get the nth node from the last of a linked list*/ int getNthFromLast(struct Node* head, int n); /* Driver program to test above function*/ int main() { int T,i,n,l,k; cin>>T; while(T--){ struct Node *head = NULL, *tail = NULL; cin>>n>>k; int firstdata; cin>>firstdata; head = new Node(firstdata); tail = head; for(i=1;i<n;i++) { cin>>l; tail->next = new Node(l); tail = tail->next; } cout<<getNthFromLast(head, k)<<endl; } return 0; }// } Driver Code Ends /* struct Node { int data; struct Node *next; Node(int x) { data = x; next = NULL; } }; */ //Function to find the data of nth node from the end of a linked list. int getNthFromLast(Node *head, int n) { // Your code here int count=0; Node* i=head; Node* t=head; while(i!=NULL) { count++; i=i->next; } int c=1; if(n>count ) return -1; count = count-n; for(int k=0;k<count;k++) t=t->next; return t->data; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter