Sunday, August 2, 2009

What is the optium way to find the middle node in a single linked list using C?

There are two ways


Method 1


1- Iterate through the link list to find the number of nodes in it (say n)


2- Start from the root of the list and travel as far as n/2





The other method is a bit tricky


Method 2


1- Start with two pointers both pointing to root


2- Now make one traverse the link list one node at a time


3- Make the other traverse the link list 2 nodes at a time


At the time, the 2nd pointer reaches the end, the 1st pointer would only have reached the mid





The 1st method is optimized for memory and the 2nd one for concurrent traversal and speed





The previous answer is incorrect. This is not a search problem. Its a traversal problem. Also a linear singly linked list cant be "binary search"-ed. You need to form a "skip list" to do that!

What is the optium way to find the middle node in a single linked list using C?
you should use binary search. if you don't know how to do binary search mail me at shantanubasu123@yahoo.com


the subject must be binary search.


No comments:

Post a Comment