When I read your post's title, I thought you were talking about answers. Well, comments aren't a big deal.
What to do when I see a comment that potentially answers a question that should've been closed?
In most cases, nothing. Almost all of the comments I've seen tend to provide hints rather than spoonfeeding the answer outright. Those require some thinking on the OP part, which sometimes may not happen, but renders them harmless, and even useful. If an OP takes time to process the feedback under their question, chances are they are or will be an avid contributor.
In the rare cases like the one you mentioned, which usually occur when the commenter is not familiar with our policies, you should comment and politely ask them not to answer homework questions since it might encourage more of them, preferably while linking to our homework policy or discussions about why we close questions instead of answering them.
What to do when I see an answer to a question that should've been closed?
There are controversial opinions about this. The only thing people seem to agree upon is not to answer off-topic questions. But again, that's meta consensus, and some people will keep ignoring it. Some people take it a step further and downvote answers to off-topic questions. But since that rule always seemed extreme, while it may look like meta consensus, not everyone is doing it.
What we're a bit consistent at doing on Chem, however, is to comment below the answer and ask them not to do it next time. I've seen canned comments like this, and prolly have left some of those myself:
While we appreciate your contribution to the site, it would be preferable to not answer questions that do not comply with our homework policy.
The raw text for use is in the Comment Templates meta post.