search from the front and then from the end to find the two nodes