The nearest train station to Sperlonga is in Fondi and is called Fondi-Sperlonga, and you can reach it by bus from Sperlonga.
From Rome take a train to Camucia-Cortona, check here for timetables, there are also direct trains from Rome to the train station called Camucia-Cortona. Once at the station of Camucia-Cortona, a bus will take you up in the historic center in few minutes.
I remain at your disposal,
Enjoy your stay in Italy,