Moderator deletions are there to enforce policy that is decided on by the community; the example you link to is a clear example of that.
If:
- you feel that a specific moderator action (deletion or otherwise) steps outside of the community-consensus guidelines, or
- you feel that the community-decided policy should be changed,
then you should handle that by posting on this meta making your case for the relevant option.
In this specific case, your answer was deleted because the community has decided that we don't want this site to provide answers to homework questions. The deletion is completely within that policy. If you want the policy to change and you want this site to allow this type of answers, feel free to make a proposal - but please make sure to get a good understanding of the considerable background of that debate and be aware that proposals that don't provide answers to the existing requirements and tensions are likely to be entirely ignored.
A bit more specifically, though, you can ask for your answer to be undeleted in a week or two, as specified by the policy. If this doesn't happen on its own, flag your post for moderator attention. If you want it to be undeleted earlier, then edit it yourself and flag for moderator attention.
More generally, if you don't want your time and energy to be "wasted", as you say, then the simplest way is very easy: don't provide full answers to homework questions, and play by the site rules.