The accurate prediction of the remaining useful life (RUL) of lithium-ion batteries is essential to improve the safety of battery systems and optimize battery management strategies. Therefore, a new algorithm combining Sparrow search algorithm (SSA) improved by the Levy flight method and long short-term memory neural network (LSTM) is proposed in this paper. By extracting the National Aeronautics and Space Administration (NASA) battery aging datasets, the battery correlated health characteristics are obtained, and the deterioration trend of battery performance is effectively described. The sparrow search algorithm improved by the Levy flight method enhances the ability of the algorithm to jump out of the optimal solution. By using the improved algorithm, the hyper-parameter optimization problem of the long short-term memory network is solved, and the accuracy of the algorithm is improved. Finally, the probability density function is used to predict the remaining useful life of lithium-ion batteries, which effectively prevents the uncertainty caused by a single prediction result. Compared with long short-term memory neural network and the one optimized by a traditional sparrow search algorithm, the mean absolute error (MAE), root mean square error (RMSE), and mean absolute percentage error (MAPE) of the proposed algorithm are kept at 1.77 %, 2.24 %, and 1.25 %, respectively. The results indicate that the algorithm has better accuracy and fitting performance.