@rogerfan123
I find it cringe worthy how often religious people who claim to be devout believers resort to shallow compromises at the slightest temptation, especially when it comes to sexual matter. (I'm looking at you, fellow catholics!) I thus applaud you, OP, for admitting that what you've done is wrong and seeking a way to stop sinning instead of playing games with half-baked excuses like the ones found on the first page.
Even though your problem with masturbation seems hard to solve as you have considered various alternatives (even going out on a late night walk) to escape the feeling of temptation via distracting yourself, the solution is pretty simple, just don't do it.
There's really no other real solution, just build up the discipline (that you no doubt have since you claim to be a good student) and don't act on the temptation. Masturbation isn't a passive thing that you just can't help but experience. It requires a lot of effort on your part as you have to touch yourself in a very specific way, while focusing on visual (or any other) material that you had to look up or go through the hassle of getting in other ways.
There's a whole job to be done there so just don't commit to it. I always thought not doing something is the easiest thing but apparently people fail to do that easiest thing and end up doing a lot that makes them feel bad later on. If it's just the feeling of arousal that's leading you to sin, then just stop playing mindgames with yourself (how do I distract myself from this) and just don't do it.
Of course, you said yourself that it's not just a physical thing and that it feels like your whole thought process changes (assuming you mean you just can't think straight) which is all the more reason to listen to my previous advice and just don't try to think your way through it. You can't think straight after all so of course you won't be able to outsmart your body.
Still, noone resists sin on their own. It isn't our arguments or wit that ultimately help us from falling down (in fact more often than not we use these to rationalize sin as can be seen on this thread). What really helps us persist in purity is our faith, faith in God, trust in God. When temptations arrive, we ought to focus on God and not try to make compromises with him but simply remember His word.
You know that God doesn't want you to sin, you know his word demands purity so stand firmly on his word and don't do what you're tempted to. It'll be hard, but so long as you know that God doesn't want you to do it just do the 'easiest' thing and don't do anything.
This doesn't just go for masturbation but every emotionally fueled sin such as lashing out at someone in a fit of rage. Don't let these things control you (you believe you have free will after all) and when confronted with these heavy urges just remember that God doesn't want you to do this and simply don't act on it.
Hope you found this helpful, I'm willing to talk further if you wish.