C Puzzles

yet another place for C puzzles

Friday, March 19, 2010

 

Finding the Nth-node-from-the-end of a linked list.

answer in my mind:

1. travel the linked list for n times
(if you hit null in between, return null)
2. Then travel the linked list till end
(remember to increment the n-node pointer along with traversal)

Comments:
http://stackoverflow.com/questions/2345347/finding-the-nth-node-from-the-end-of-a-linked-list
 
its very simple..
take two pointers p1,p2
start p2 after p1 has travelled 'n' nodes and let p1 travel upto last.the node pointed by p2 will be nth node from last
 
Post a Comment

Subscribe to Post Comments [Atom]





<< Home

Archives

July 2005   August 2005   October 2005   December 2005   March 2006   June 2006   July 2006   December 2006   February 2007   June 2007   March 2010   May 2010  

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]