If the flickering lamps are wired such that they do NOT have a return path directly (mechanically) through the frame of the car, then you can use the following method. (If they are connected mechanically to provide the return, you will need to isolate the return of each lamp and provide a separate return wire for each.)
The method involves taking the hot and return leads (roller and frame) and using them as the A-C input to a full-wave bridge rectifier. This is accomplished by soldering four ten-cent diodes into the required bridge, or buying a one-dollar ready-made bridge. The D-C output of the bridge is connected in parallel (plus to plus, negative to negative) to a 30 volt, 1000 uFd electrolytic capacitor. The output of this circuit powers the lamps. The cap acts as a filter, or short-term storage battery, smoothing out the peaks and valleys of electricity as the train moves down the track. The scheme only works with D.C., hence the need for the bridge rectifier.
I'll wager that this explanation has appeared many times in this and other forums, so use the search feature for other opinions on the theme.