As the answer is already posted I'm not going to give a numerical answer, but rather explain how the problem shoul be approached.
The thing with this problem is that if you try to solve it from the fly's point of view the calculation of the distance travelled gets really though.
This is due to the fact that the distance the fly travels each time is reduced as the trains keep approaching each other at constant speed. So you should relate the movement of the trains with the movement of the fly.
However, if you could only figure out the time spent by the fly traveling between the two trains, then it gets a lot easier as: $$distance[km]=time[h]·speed[\frac{km}{h}]$$
(because the fly moves at a constant speed and suposing the initial distance traveled equals $0$.)
So we know the fly's speed, but how do we get the time? Well, the time the fly is going to spend traveling between trains is the same as the time that will pass until trains collide isn't it? And this is much easier to calculate...