Yes. If you find that you can make the answer more complete and correct, then absolutely edit it.
However, I'd like to add one thing that the other answers haven't mentioned here. The OP accepted your original answer, so it must have been useful to them (and therefore would probably be useful to others). If your new solution is significantly different from the original answer (i.e. not just typos, or silly mistakes you overlooked the first time around), then I'd suggest including it as an addendum under your original answer. You never know, maybe for some reason the first answer worked better for the OP than your second one would. So for the sake of completeness, I prefer to keep the accepted solution as well as my corrected / updated solution, just in case someone finds the original useful, as well.