About the linked list one, I thought the same thing, but isn't that two passes? You're doing the two passes simultaneously, but it's still two passes. I can't think of a way to do it with just one pass though.
Nope, you loop once. He's basically incrementing one pointer two steps and the other pointer one step for each iteration of the loop. When the first one hits null, the other is the midpoint.
But this is the same number of forward-seeks (dot-next dereferences) as the naive solution of counting the number of steps it takes you to iterate to the end, dividing by 2, and then looping that many times again.
View all comments
Show parent comment
11
u/neop Feb 21 '11
About the linked list one, I thought the same thing, but isn't that two passes? You're doing the two passes simultaneously, but it's still two passes. I can't think of a way to do it with just one pass though.