There are, of course, different ways to approach and interpret madness in these plays. One could argue that madness in both plays is a response to corruption. In Hamlet, Hamlet must cope with the possibility—and then the reality—that his uncle murdered his (Hamlet's) father to gain the throne. This realization of his uncle's capacity for evil makes Hamlet aware of the pervasive evil—described in the play as a disease or rot—permeating the entire Danish court. The world seems to Hamlet to have gone mad, and he hardly knows where to turn.
Likewise, King Lear must deal with the treachery of his close relatives, in this case his two eldest daughters, who lied to him to get his power and now are willing to cast him off and let him die. His world, too, seems to have gone mad. He can hardly believe his own children would treat him the way these two have.
Both Hamlet and Lear respond to worlds that seem to have gone mad by experiencing spells of madness themselves. Lear goes into a frenzy of anger and madness during the storm on the moor. Hamlet goes into a frenzy of anger and madness in Gertrude's chamber, during which he mistakenly kills Polonius.
It is said in psychology that the healthiest member of a dysfunctional family will often be labelled the "crazy" one by the others, because he or she calls out or names the dysfunction that everyone wants to pretend doesn't exist. Both Hamlet and Lear play this role in their respective plays. While acknowledging their own flaws, both cling to a sound moral worldview despite being surrounded by evil, and this moral compass paradoxically drives them to temporary madness when they confront the evil in their own families and courts.