C Puzzles

yet another place for C puzzles

Friday, June 02, 2006

 

Middle of singly linkedlist in one traversal.

you have given one singly linked list.
you have to find its middle traversing it ONLY once

Comments:
Take two pointers pointing to head of link list then increase one pointer by one and second by two and when second pointer points to null then first one will be at middle of the link list.
 
exactly!!
 
Doesn't that traverse the list, um, 1.5 times?
 
there should be two conditions:
- number of the elements in linked list is even
- number of the elements in linked list is odd

that traversing has to be 1.5 times. cos we need to use two pointers, one is traversing at the speed of twice of the first one.
 
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]