By Ayesha Rascoe and Angus Berwick

MADRID (Reuters) - U.S. President Barack Obama met the king of Spain and acting Prime Minister Mariano Rajoy on Sunday, but the trip that was meant to show solidarity with Europe has been overshadowed by violent events in the United States.

The visit is Obama's first to Spain as president. White House officials said it was important for Obama to make the trip, because Spain, which has been without a functioning government since December, was the only major European country he had not traveled to during his presidency.

Obama was supposed to spend two days in Spain after attending a NATO summit in Warsaw where the United States, Spain and other allies pledged to stand united in the face of threats from Russia and fallout from Britain's vote to leave the European Union.

But, after a sniper killed five police officers in Dallas on Thursday following the fatal shootings of two black men by police in Louisiana and Minnesota, the White House cut short the trip so Obama could go to Dallas.

Plans for sightseeing in Seville and a town hall meeting with Spanish citizens were canceled. Instead Obama, who landed in Madrid late on Saturday night, squeezed in sessions with King Felipe VI and acting Prime Minister Mariano Rajoy on Sunday.

He will also address troops at Naval Station Rota in the southern region of Andalusia, before heading back to the United States on Sunday night.

Speaking alongside King Felipe at Madrid's royal palace, Obama said he wished he could stay longer in Spain, which he said he first visited just before entering law school in his 20s when he was backpacking across Europe.

"We have had a difficult week back in the United States, so my trip is a little abbreviated but I thought it was very important for me to come here, given the extraordinary friendship and alliance between Spain and the U.S.," he said.

King Felipe, who visited Obama in the White House last year with his wife Queen Letizia, said Spain was committed to the closest possible cooperation with the United States.

In an interview with Spain's El Pais published on Saturday, Obama called Spain "an indispensable European partner."

"Spain is a strong NATO ally, we're grateful for Spain's many decades of hosting U.S. forces, and we're major trading partners," Obama said in the interview. "That's why the United States is deeply committed to maintaining our relationship with a strong, unified Spain."

Spain has been stuck in a near seven-month political stalemate since a national election in December stripped Rajoy of his majority and forced parties to negotiate, so far without success, about forming a coalition government.

Rajoy's conservative People's Party (PP) failed to win a parliamentary majority in a repeat election last month, meaning the deadlock is set to continue with the possibility of a third round of elections in the future.

"If we have to repeat the elections it would be a joke that would affect not only the Spanish economy but the credibility of our country both here and abroad," Rajoy told a news conference with Obama at his official residence.

Obama said he wanted to see a "stable" Spain, but the relationship between both countries would not depend on which party was in power. He met with the leaders of the three largest opposition parties, the White House said in a statement.

Obama also spoke about the violence in Dallas, warning that attacks on police over racial bias would hurt the anti-racism Black Lives Matter movement.

(Additional reporting by Maria Vega Paul; Editing by Sandra Maler and Raissa Kasolowsky)