You sure could. Make sure the 12V supply is able to accept current flowing into it, as well as providing current flowing out.
But it's more efficient to use an "H-bridge" circuit to reverse both ends.
To move in one direction, you put one end at 0V and the other end at 24V.
To move in the other direction, you put one end at 24V and the other end at 0V.
The half-H-bridge is just a pair of switches, one to 24V and one to 0V, so that the output can be either 24V or 0V depending on which one you turn on. Turning on both at the same time creates a short circuit, so don't do that. The H-bridge is just two halves, one for each end of the solenoid.
Of course, the solenoid has to be mechanically designed to work in both directions, too. There's no point moving a solenoid both ways, if one way causes it to bump into an end stop and not move.
The solenoid must use a permanent magnet in the moving piece, because iron that isn't magnetized will be attracted to either a north or south field equally well - reversing the current will reverse the magnetic field but that won't achieve anything.