When Hamlet first encounters the gravediggers, his reaction is one of disbelief at the treatment of the dead:
Has this fellow no feeling of his business, that he
sings at grave-making?
He goes on to say the following:
That skull had a tongue in it, and could sing once:
how the knave jowls it to the ground, as if it were
Cain's jaw-bone, that did the first murder! It
might be the pate of a politician, which this ass
now o'er-reaches; one that would circumvent God,
might it not?
Hamlet is questioning the actions of the gravedigger, but here, one can see that he is starting to think of humanity and the fact that all of these skulls are human and were living once. He's beginning to see that the skulls represent what everyone becomes once they die.
What really makes him realize this is when he sees Yorick's skull. Because he knew Yorick, he can make the connection to the rest of humanity and himself. Eventually, he references Alexander the Great asking:
Dost thou think Alexander looked o' this fashion i'
This suggests a change in his outlook in that he's realizing that death comes no matter what. He's starting to see that his life isn't worth preserving and that he can and must risk it in order to avenge his father's death. Once he sees that the gravediggers are digging Ophelia's grave, he has nothing to live for.