Perhaps the first question to ask here relates to the original states of the two characters at the opening of their respective tragedies. It is clear in King Lear that the monarch has at least allowed a degree of moral blindness to overtake him even before the narrative action of the play has occurred. This is clear from 1.1 where we hear that Gloucester's infidelity has been allowed to go unchecked such that he has sired an illegitimate child. Indeed, it is this moral blindness to the state of his kingdom that allows Edmund, the product of this union, to become the antagonist of much of the dramatic action. Moreover, from his first appearance, Lear is willing not only to ask what might be considered an 'insane' question of his daughters - 'which of you shall we say doth love us most'. Moreover, when he receives the insincere answers of his daughters, Goneril and Regan, he is willing to accept them for truth. Thus, whether it is his hubris (Aristotle's term for the over-arching pride of the tragic hero/ine) or the insanity that is to increasingly plague him, Lear might be argued to have a pre-existent flaw even before the play has begun.
Hamlet, on the other hand, begins the play as a student and a philosopher, a rationalist, so much so that Shakespeare deliberately makes him a student of the great German seat of learning, Wittenberg. He, like his companion, Horatio, maintains a logical scepticism throughout the play and, indeed, for all of his melancholy, he claims - and perhaps rightly - that the 'inky cloak' of his mood is caused by the death of his father and the 'hasty' second marriage of his mother; it is at least a mood that is justified by circumstance. However, in his case, it is a melancholy that, from the start, he seeks, at least intellectually, a respite from in thoughts of suicide such as in his first soliloquy where he hopes that this 'too sallied flesh would melt', a desire, of course, repeated in the most famous soliloquy of the play, when he contemplates self destruction in 3.1 rather than suffering the 'slings and arrows of outrageous fortune.'
One might argue that another fundamental difference between the two comes in that Hamlet believes himself to be sane for much of the play and, indeed, his 'antic disposition' - the guise of insanity that he feigns in order to buy himself the time to prove Claudius's guilt, using the 'mousetrap' device - allows the self-deception about his own sanity that is a counterpoint to a descent that is clearly much more gradual than Lear's. Structurally, while Hamlet is self-questioning by 3.1 of Hamlet in the 'To be , or not to be' soliloquy, Lear is by a similar point, exiled to the wilderness outside of Gloucester's castle and seemingly mostly out of his mind. However, one other point of difference comes in Lear's moments of lucidity even at the nadir of his descent into madness - in 3.2, for example, he is fleetingly aware of his own decline, stating 'my wits begin to turn'. However, for all of his increasingly erratic behaviour, one might argue that Hamlet maintains his rationality entirely and, in the end, as he says in 5.2, finds himself unable to stop the action of the tragedy as 'They had begun the play' and he finds himself stuck within the inexorable tragic action and, without anything left to do, at the end of the play, is left to play his role. One might argue, indeed, that he never possesses madness, rather a self-awareness of his predicament that is entirely sane but that he is stuck in a situation where all those around him are increasingly mad and only his death will restore moral balance to the play.
Lear, by the end of the play, might be argued to have momentarily had his sanity at least partially restored to him with his reconciliation with Cordelia only, upon her death, to have a hopeful self-deception steal back upon him when he hopes, despite seeing her hung, that she lives still. However, one might argue that this 'madness' is the madness of a hope that makes his final demise all the more painful whereas Hamlet's decline is of a different order, a melancholy that gives way to a nihilistic despair that leads him to a form of suicide in action when he fights Laertes and seeks to avenge his father at the play's denouement.