Didn’t just kill Gollum and Gandalf expresses the belief that he had some part to play still and that it was a good thing Bilbao showed mercy on him.
I doubt Sam could have stopped evil, invisible Frodo. And had bilbo
Not shown the creature mercy....
Also, it’s fitting that Gollum dies with the ring that he loved most of all. In another way of looking at it, God gives that which makes us most happy.