add head algorithm