Thursday, April 8, 2010
Binary search on a linked list
The answer is . But, the question is, do you really think ?
, real hard.
Do you know what exactly makes the binary search on an array ? Its the ability to in . This is what makes it so fast. You can get to the . Now, ? The answer is . You will have to write your own, algorithm to get the value of . In a linked list, .
One solution to the of getting the middle of the linked list during a binary search is to have the first node contain one additional pointer that points to the node in the middle. Decide at the first node if you need to check the first or the second half of the linked list. Continue doing that with each half-list.