FORT BRAGG, N.C. — Army Sgt. Bowe Bergdahl, five years a Taliban captive after abandoning his post in Afghanistan, pleaded guilty Monday to desertion and misbehavior-before-the-enemy charges that could put him in prison for life.

"I understand that leaving was against the law," said Bergdahl, who admitted guilt without striking a deal with prosecutors, leaving his punishment up to a military judge.

The guilty pleas bring the highly politicized saga closer to an end eight years after Bergdahl vanished. President Barack Obama brought him home in 2014 in a swap for five Taliban prisoners at Guantanamo Bay, saying the U.S. does not leave its service members on the battlefield. Republicans roundly criticized Obama and Donald Trump went farther while campaigning for president, repeatedly calling Bergdahl a "dirty, rotten traitor" who deserved to be executed by firing squad or thrown out of a plane without a parachute.

The judge decided that Trump's comments, though "disturbing and disappointing," did not constitute unlawful command influence by the soon-to-be commander in chief.

Bergdahl, now 31, told the judge, Col. Jeffrey R. Nance, on Monday that he now understands that his decision to walk off his remote post in 2009 prompted intense search and recovery missions, during which some service members were seriously wounded. After Bergdahl said he understands that he could be sentenced to life in prison, Nance accepted his pleas.

A military probe began as soon as Bergdahl was freed by allies of the Taliban. He said he abandoned his post with the intention of reaching other commanders and drawing attention to what he saw as problems with his unit.

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

"At the time, I had no intention of causing search and recovery operations," said Bergdahl. "I believed they would notice me missing, but I didn't believe they would have reason to search for one private."

Bergdahl was a private first-class when he vanished and received the promotions due all soldiers missing in action while he was in captivity.

By pleading guilty without a deal with prosecutors, Bergdahl must now rely on leniency from the judge. The misbehavior charge carries a maximum penalty of life in prison, while the desertion charge is punishable by up to five years.

Military law scholars say that admitting guilty without a plea agreement is risky but could bring some benefits, because he doesn't have to reach agreement with prosecutors on certain facts. For example, Bergdahl told the judge that he was pleading guilty to a period of desertion lasting until he was captured by the Taliban, while prosecutors argued Monday that he was responsible for the entire time until he was returned to U.S. authorities.

Bergdahl's answers to the judge's questions Monday represented some of his most extensive public comments yet.

He told the judge that he tried to escape from his captors between 12 and 15 times, with varying degrees of success. Once, he was on his own for about a week — hoping U.S. drones would spot him — before he was recaptured. He said he also tried to escape on his first day in captivity, but was tackled soon after he made a run for it.

"As I started running there came shouts, and I was tackled by people. That didn't go so well," he said.

Asked whether he understood his duties in Afghanistan to be important, he also reflected on what he thought were questionable tactics by U.S. soldiers and their Afghan allies to guard a remote crossroads that could be bypassed by the Taliban on other routes. He said the setup "seemed to be a bit of a joke. ... At the time it was very hard for me to understand."

Still, pressed by the judge about his actions, Bergdahl acknowledged endangering his fellow service members.

"I left my platoon in a battlefield ... a situation that could easily turn into a life or death situation," he said.

While Berghdahl's pleas enable him to avoid a trial, he'll still expected to face a sentencing hearing later this month. His years in captivity could be factored in, but the hearing also will likely feature damning testimony from his fellow service members. A Navy SEAL who suffered a career-ending leg wound and an Army National Guard sergeant whose head wound put him in a wheel chair would not have been hurt in firefights had they not been searching for Bergdahl, the judge has ruled.

The defense also was rebuffed in an effort to prove that Trump had unfairly swayed the case. The judge ruled in February that the new president's comments were "disturbing and disappointing" but did not constitute unlawful command influence by the soon-to-be commander in chief.

Bergdahl, who's from Hailey, Idaho, has been assigned to desk duty at a Texas Army base while his case unfolds.