Shares of Sprint soared Tuesday after a U.S. district judge ruled in favor of its $26 billion deal to merge with T-Mobile.

The stock was up 77.7% Tuesday. It had risen in extended trading Monday after The Wall Street Journal reported the judge was expected to rule in favor of the deal. Shares of T-Mobile were up 11.8%.

The ruling clears one of the final hurdles for the deal, which still can't close until the California Public Utilities Commission approves the transaction. Tuesday's ruling also culminates a yearslong courtship between Sprint and T-Mobile, which have made multiple attempts over the years to merge, only to abandon their plans fearing regulatory scrutiny.

Attorneys general from New York, California, Connecticut, Hawaii, Illinois, Maryland, Michigan, Minnesota, Oregon, Wisconsin, Massachusetts, Pennsylvania, Virginia and the District of Columbia originally brought the lawsuit to block the deal following approval from the Justice Department and the Federal Communications Commission. The states had argued that combining the No. 3 and No. 4 U.S. carriers would limit competition and result in higher prices for consumers. The companies had contended their merger would help them compete against top players AT&T and Verizon, and advance efforts to build a nationwide 5G network.

In his decision filed Tuesday, Judge Victor Marrero wrote, "The resulting stalemate leaves the Court lacking sufficiently impartial and objective ground on which to rely in basing a sound forecast of the likely competitive effects of a merger."

The judge laid out three points on which the court rejected the states' objections to the merger. First, he said, they failed to convince the court that the merged party "would pursue anticompetitive behavior that, soon after the merger, directly or indirectly, will yield higher prices or lower quality for wireless telecommunications services."

Second, the court rejected that Sprint would be able to continue operating effectively as a wireless services competitor without the merger.

"The Court is thus substantially persuaded that Sprint does not have a sustainable long-term competitive strategy and will in fact cease to be a truly national [mobile network operator]," the ruling said.

And finally, the court rejected the states' argument that Dish Network "would not enter the wireless services market as a viable competitor nor live up to its commitments to build a national wireless network." The deal called for Dish to step in as a new wireless player based on agreements with the DOJ and FCC. Shares of Dish were up 7.1% on the judge's ruling.

In a statement after the ruling, New York Attorney General Letitia James, who helped lead the states' push, said the states "disagree with this decision wholeheartedly, and will continue to fight the kind of consumer-harming megamergers our antitrust laws were designed to prevent." She called the ruling a "loss" for Americans who rely on wireless networks and said the states will review their options, including a potential appeal.

"From the start, this merger has been about massive corporate profits over all else, and despite the companies' false claims, this deal will endanger wireless subscribers where it hurts most: their wallets," James said.

California Attorney General Xavier Becerra, who also led the states' efforts, said in a statement: "Our fight to oppose this merger sends a strong message: even in the face of powerful opposition, we won't hesitate to stand up for consumers who deserve choice and fair prices. We'll stand on the side of competition over megamergers, every time. And our coalition is prepared to fight as long as necessary to protect innovation and competitive costs."

Makan Delrahim, who leads the DOJ Antitrust Division, said in a statement he was "grateful that the judge recognized the expertise of the Department of Justice and the Federal Communications Commission (FCC) in his evaluation of the transaction."

The case has seemed to escalate tensions between some of the state attorneys general and the DOJ, who are each pursuing investigations into Google. "As I have noted before, should a minority group of states, or even one, be able to undo the nationwide relief secured by the federal government, it would wreak havoc on parties' ability to merge, on the government's ability to settle cases, and cause real uncertainty in the market for procompetitive mergers and acquisitions," said Delrahim, who is recused from the DOJ's Google probe.

Top executives at T-Mobile and Sprint claimed a victory with the judge's ruling.

"[N]ow we are finally able to focus on the last steps to get this merger done!" T-Mobile CEO John Legere said in a statement.