Well, for this what you can do is to traverse the list
from the end. Keep a counter and keep checking for the change in the object. As soon as you detect a change, replace the number in the counter with the current position + 1
. Do this till you reach the first position.