If you are looking for one-and-done then by all means get a purpose-built reversing controller as Mike suggests.
OTOH if you are game for DIY tinkering, you can cobble together something using relay timer modules. For example, this was discussed in this OGR thread. This defines the problem as a widget that momentarily interrupts track power for, say, 1 second (to put the loco in neutral) ... then waits a few seconds ... then interrupts track power again for 1 second (to restart the loco in other direction). This sequence would be triggered when the loco reaches either end.
The timer module referred to in the linked thread is widely available.
There would be no bells and whistles like the DZ-1240 which appears to be about $60.
I'd think it could be done for $10-20; I only suggest this to those that see this kind of messing around as part of the fun of the hobby.