Classification of electromagnetic and gravitational Hopfions by Algebraic type