A uncharged capacitor C is connected to a battery with potential V
Since the voltage across a capacitor must be continuous (for finite current), one cannot specify this and expect to get valid answers.
Remember, for the ideal capacitor
$$i_C = C \dfrac{dv_C}{dt}$$
But specifying that an "uncharged capacitor C is connected to a battery with potential V" is equivalent to specifying
$$v_C(t) = Vu(t)$$
But this is equivalent to specifying
$$i_C(t) = CV\delta(t)$$
which certainly ejects this problem outside of the realm of ideal circuit theory.
Now, there is an easy remedy for this - place a resistor in series with the capacitor and then the capacitor voltage becomes
$$v_C(t) = V(1 - e^{-\frac{t}{RC}})u(t)$$
and the current is finite
$$i_C(t) = \frac{V}{R}e^{-\frac{t}{RC}}u(t)$$

Now, it is straightforward to show that the energy stored in the fully charged capacitor is less than the energy supplied by the battery with the difference being the energy dissipated by the series resistor.
Finally, $R$ cannot be reduced to zero - one can show that there is a fundamental radiation resistance associated with the circuit such that energy from the battery can be radiated away during charging.
My question is: What is the work done by the the battery?
When the polarity is reversed, the capacitor will initially discharge, doing work on the battery, until fully discharged and then the battery will again begin doing work on the capacitor.
Since there is loss during the charging / discharging process, one cannot equate the work done by the battery to the work done on the capacitor.
Perhaps the problem is to be solved assuming that the capacitor
connected to a battery gets approximately a charge CV(in negligible
time).
Including a series resistor allows one to compute the energy dissipated by the resistor which turns out to be independent of the resistance and equal to
$$W_R = \frac{CV^2}{2}$$