Towards the end of the play, Hamlet’s sanity can truly be questioned as he experiences bursts of madness and total reason. Hamlet kills Polonius, whether cold-bloodily or accidentally, and feels no remorse as he drags the body away. This contributes to Hamlet’s madness although when he is brought to the King, his remarks are logical. Regardless, Claudius sends Hamlet away to avoid any problems regarding Polonius’ death, but gives Rosencrantz and Guildenstern instructions to kill Hamlet. Hamlet seems strangely aware of what is going on, and anticipating Claudius’ move, responds by replacing the orders for his death with the orders for the death of those who carry the letters. This shows careful planning and sanity. When returns to Denmark, he shows signs of insanity when he bursts out of hiding as he realizes that Ophelia has died. Right before his duel with Laertes, he apologizes to him. He claims to Laertes that Hamlet’s madness is both of their enemies and therefore it is Hamlet’s madness that’s Laertes’ enemy, not Hamlet. This could be Hamlet’s understanding that his madness throughout the play was not entirely an act or it could just be an excuse for Hamlet’s actions. The entire duel, Hamlet shows no signs of madness but instead shows his skill with the sword.
At his death, Hamlet shows no signs lunacy, but rather shows foresight by giving his support to Fortinbras as the next king. It is never evident whether Hamlet is entirely mad or not because at the beginning of the play he says he will feign madness but later blames madness for his actions. His madness could have been completely feigned but it is also possible that the death of his father and Ophelia and the marriage of her mother actually caused him to lose his mind, as he sometimes claims.
Short background on Hamlet:
Hamlet is a tragedy by William Shakespeare, believed to have been written between 1599 and 1601. The play, set in Denmark, recounts how Prince Hamlet exacts revenge on his uncle Claudius, who has murdered Hamlet's father, the King, and then taken the throne and married Hamlet's mother. The play vividly charts the course of real and feigned madness—from overwhelming grief to seething rage—and explores themes of treachery, revenge, incest, and moral corruption.
Despite much literary detective work, the exact year of writing remains in dispute. Three different early versions of the play have survived: these are known as the First Quarto (Q1), the Second Quarto (Q2) and the First Folio (F1). Each has lines, and even scenes, that are missing from the others. Shakespeare probably based Hamlet on the legend of Amleth, preserved by 13th-century chronicler Saxo Grammaticus in his Gesta Danorum and subsequently retold by 16th-century scholar François de Belleforest, and a supposedly lost Elizabethan play known today as the Ur-Hamlet.
Given the play's dramatic structure and depth of characterization, Hamlet can be analyzed, interpreted and argued about from many perspectives. For example, scholars have debated for centuries about Hamlet's hesitation in killing his uncle. Some see it as a plot device to prolong the action, and others see it as the result of pressure exerted by the complex philosophical and ethical issues that surround cold-blooded murder, calculated revenge and thwarted desire. More recently, psychoanalytic critics have examined Hamlet's unconscious desires, and feminist critics have re-evaluated and rehabilitated the often maligned characters of Ophelia and Gertrude.
Hamlet is Shakespeare's longest play and among the most powerful and influential tragedies in the English language. It provides a storyline capable of "seemingly endless retelling and adaptation by others". During Shakespeare's lifetime, the play was one of his most popular works, and it still ranks high among his most-performed, topping, for example, the Royal Shakespeare Company's list since 1879. It has inspired writers from Goethe and Dickens to Joyce and Murdoch and has been described as "the world's most filmed story after Cinderella". The title role was almost certainly created for Richard Burbage, the leading tragedian of Shakespeare's time. In the four hundred years since, it has been played by highly acclaimed actors, and sometimes actresses, of each successive age.
*Background directly from Wikipedia*