GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 110 of 203    Print  
Find nth node from end of a singly linked list

  
Total Answers and Comments: 3 Last Update: October 19, 2009     Asked by: smart_coder 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: Apoorv
 
A simple way out would be..Use two pointers..*traverse and *nth_positionboth initialised to null..While you traverse to the end of the list using *traverse,let *nth_position follow only after n iterations..So at all times, *nth_position will be n places behind *traverse..Continue till traverse reaches end of list..If list has less than n nodes, nth_position points to null.

Above answer was rated as good by the following members:
yzesong, j_l_larson
February 09, 2007 23:07:08   #1  
Abi George Ullattil        

RE: Find nth node from end of a singly linked list
struct node{ int data; node* next;};// This function will return the point to the nth node from the end.// If the number of links is less the value n then it returns the start of the linked list// If the linked list has only one node then it returns the link to the end node// (which again is the start node for that linked list)node* findNthNodeFromEnd(node *end int n){ if(!end || n<0) return NULL; else if(n 0) return end; char *p; node *curr *prev end; int i 0; curr prev - 1; p (char*)curr; while(p ! 0) { if(curr->next prev) { ++i; if(i n) return curr; prev curr; } --p; curr (node*)p; } return prev;}The explanation for this one should be easily understandable i guess. If you have doubt mail me.
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
February 16, 2007 00:07:18   #2  
Apoorv        

RE: Find nth node from end of a singly linked list
A simple way out would be..Use two pointers..*traverse and *nth_positionboth initialised to null..While you traverse to the end of the list using *traverse let *nth_position follow only after n iterations..So at all times *nth_position will be n places behind *traverse..Continue till traverse reaches end of list..If list has less than n nodes nth_position points to null.
 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
October 19, 2009 02:25:56   #3  
j_l_larson Member Since: June 2008   Contribution: 4    

RE: Find nth node from end of a singly linked list
node*GetNthFromEnd(node* pHead int n) {
if ( ! pHead )
return NULL;
int i 0;
node* p pHead;
node* pnth pHead;
while (p) {
if ( i++ > n ) {
pnth pnth->GetNext();
}
node* pNext p->GetNext();
p pNext;
}
if ( i < n )
return NULL;
return pnth;
}


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape