Note: the condition `prevIndex=0` always passes.