Before his journey to England, Hamlet is confused and depressed and in touch with his raw, damaged emotions. In the immortal 'to be' speech, he sensitively weighs up the value of life against the value of death. He is sick of the world and wants to leave it. But the option of death looks uncertain to him. He imagines many different things about the afterlife, "for in that sleep of death what dreams may come?", "the undiscovered country from whose bourne no traveller returns". It puzzles him. It interests him. He sees death as a surreal and strange journey into the unknown.
Later in the play he returns from England and he has cut himself off from his emotions. You might say he has grown up, but that isn't necessarily a good thing. He holds the silent, mouldy skull of Yorick, a court-jester who once was a laugh-loving, joke-cracking, party animal. Hamlet knew him as a child, and feels nothing personal as he stares into the empty eye-scokets of someone he once loved.
And he sees nothing in Yorick's lifeless head except decaying organic matter that one day may be recycled as something as lifeless and ordinary as clay wall-covering.
By the time Hamlet arrives in the graveyard in Act 5, he is no longer considering suicide as an option to action. The last time we see Hamlet before he leaves for England, he has made a very determined statement of intent to achieve his goal of revenging his father's death. Also, in many of Shakespeare's plays characters go into the proverbial forest and re-emerge completely changed. In this case, Hamlet goes onto the sea and becomes a more pro-active version of himself. When Hamlet sees Yorick's skull he becomes very aware of death as the great equalizer. No matter who you are, no matter how low or how powerful, you will die. With this realization becomes an acceptance of what needs to be done.