In a surprise announcement, Mayor Michael R. Bloomberg said Thursday that Hurricane Sandy had reshaped his thinking about the presidential campaign and that as a result, he was endorsing President Obama.

Mr. Bloomberg, a political independent in his third term leading New York City, has been sharply critical of Mr. Obama, a Democrat, and Mitt Romney, the president’s Republican rival, saying that both men had failed to candidly confront the problems afflicting the nation. But he said he had decided over the past several days that Mr. Obama was the better candidate to tackle the global climate change that he believes might have contributed to the violent storm, which took the lives of at least 38 New Yorkers and caused billions of dollars in damage.

“The devastation that Hurricane Sandy brought to New York City and much of the Northeast — in lost lives, lost homes and lost business — brought the stakes of next Tuesday’s presidential election into sharp relief,” Mr. Bloomberg wrote in an editorial for Bloomberg View.

“Our climate is changing,” he wrote. “And while the increase in extreme weather we have experienced in New York City and around the world may or may not be the result of it, the risk that it may be — given the devastation it is wreaking — should be enough to compel all elected leaders to take immediate action.”