There primary purpose is to prevent damage or derailment of the train.
There are lifeguards Infront of the first set of wheels on trains in my country, your neck would have to clear this (very unlikely) to be ran over by the wheels.
I once had the naïve idea that I'd just need to whack my neck on the tracks and then it would be over.
Railway suicides are often much less predictable than that, front locomotives are designed to displace obstacles on the rail.
I've seen some quite clean attempts that have targeted wheels on later carriages, events in Russia and the far East.
I Imagine it's just sheer kinetic energy in most successful attempts but I don't know, I'm often surprised by how they pan out.
Many don't like this method because you effectively force others to participate in your attempt.