“Theresa May doesn’t look happy on the campaign trail,” said Mark Wickham-Jones, professor of political science at the University of Bristol. “And Labour have proved quite effective at chipping away at things like her reluctance to debate.”

At the same time, Mr. Corbyn, who survived an attempt last year by his own members of Parliament to unseat him as Labour leader, had a very good campaign. Appealing to the young, especially in the big cities, Mr. Corbyn ran on a platform promising more social justice, free college tuition, more money for the National Health Service and welfare, the re-nationalization of the railways and utilities, and much higher taxes on corporations and those earning over £80,000, about $104,000, a year.

His performances on television were calm and avuncular, with a touch of humor. And as the campaign wore on, he appeared to win back the support of most Labour voters in 2015, plus some Liberal Democrats and Greens.

The polls narrowed. But the Conservatives never lost their lead in any major poll. And party professionals on the ground, especially in marginal seats in the Midlands and the north that the Conservatives had targeted, reported continuing resistance to Mr. Corbyn as a credible prime minister.

The campaign was also marred by two terrorist attacks that caused numerous casualties, in Manchester on May 22 and then, last Saturday, in London. These also seemed to work against Mrs. May, at least at first. As home secretary for six years before becoming prime minister, she was criticized for the security services’ failure to stop the plots and for supporting cuts in beat policing.

Yet, late polling indicated that she benefited from her tough response — especially after the London attack, when she promised new counterterrorism legislation — and had widened the gap with Labour at the end.

The candidates spent the last day of official campaigning racing around the country — Mrs. May by jet, Mr. Corbyn by train. “They underestimated us, didn’t they?” he told a rally in Glasgow.