Check that prev is not null to avoid NPE.