The spectra of charged particles produced within the pseudorapidity window |η| < 1 at s NN √sNN =5.02 TeV are measured using 404 μb−1 of PbPb and 27.4 pb−1 of pp data collected by the CMS detector at the LHC in 2015. The spectra are presented over the transverse momentum ranges spanning 0.5 < pT< 400 GeV in pp and 0.7 < pT< 400 GeV in PbPb collisions. The corresponding nuclear modification factor, RAA, is measured in bins of collision centrality. The RAA in the 5% most central collisions shows a maximal suppression by a factor of 7-8 in the pT region of 6-9 GeV. This dip is followed by an increase, which continues up to the highest pT measured, and approaches unity in the vicinity of pT = 200 GeV. The RAA is compared to theoretical predictions and earlier experimental results at lower collision energies. The newly measured pp spectrum is combined with the pPb spectrum previously published by the CMS collaboration to construct the pPb nuclear modification factor, RpA, up to 120 GeV. For pT> 20 GeV, RpA exhibits weak momentum dependence and shows a moderate enhancement above unity.