17-year-old Brandon McInerney on Monday pleaded guilty to the second-degree murder of gay teen Lawrence King, the AP reported.

The plea comes just two months after a judge in California declared a mistrial after jurors could not decide whether McInerney, then 14, deserved to be convicted of voluntary manslaughter or first-degree murder.

McInerney pleaded guilty to the murder charge and one count each of voluntary manslaughter and use of a firearm, which will send him to prison for 21 years and avoid a retrial. He is scheduled to be sentenced on December 19.

King, 15, was shot twice in the back of the head during an E.O. Green Junior High School computer class on the morning of February 12, 2008. He died in the hospital on Valentine's Day.

During the trial, defense lawyers argued that McInerney suffered abuse at the hands of his drug-abuser father, and didn't deserve to be tried as an adult.

Family members testified that the McInerneys divorced in 2000 amid allegations of spousal abuse and drug addiction, and that the father, who has subsequently committed suicide, often humiliated his son in public.

King told friends and family he was gay and often wore makeup to school. Students testified that he either had a crush on McInerney or delighted in teasing the boy, asking him, “What's up baby?” the day before King's murder.