Optimized algorithm for building neighbor list.