NEW YORK — On July 1, 2016, as he contemplated his basketball future with nearly a dozen others inside a room in his hometown of Palo Alto, California,Jeremy Lin ended up pulling an all-nighter.

“I couldn’t sleep,” Lin said. “I was too excited.”

After meeting with the Brooklyn Nets and New Orleans Pelicans at the start of the NBA’s free-agency period at 9:01 p.m. PT, the unrestricted free-agent point guard sat down with his agents, family members and close friends to decide his future.

Hours of discussion and prayer followed.

At 7:02 a.m. PT, Lin announced on Twitter that he was signing with the Nets, reuniting with Kenny Atkinson, Brooklyn’s head coach who had been Lin’s mentor when Atkinson was an assistant with the Knicks.

Lin said he made up his mind sometime in the middle of the night.

The journey continues…thankful for the next chapter!!

“This is where I want to be,” said Lin, who inked a three-year, $36 million deal with Brooklyn that includes a player option in the final year. “I feel this is the best place for me to become the player I think I can become. I want to see how good I can become and be a big part of an organization, and that’s what they gave me here.”

And to think, most teams didn’t want anything to do with him last offseason.

Hitting a low point

Exactly a year earlier, after a season gone wrong with the L.A. Lakers, Lin had no suitors.

He felt the way he had felt before “Linsanity,” when he was simply trying to hang on for his NBA life. His joy for the game had evaporated.

“I couldn’t even get the minimum from certain teams,” Lin said. “That showed me how low my market value was. Obviously, I was offended. I just couldn’t understand it.”

The Nets had targeted Lin with their mini-midlevel exception, sources said, but according to Lin, “they were never legitimately a contender in terms of where I wanted to go.”

Brooklyn wound up splitting its mini-midlevel between Shane Larkin andWayne Ellington.

Lin wanted to play in Dallas, but the Mavericks brought in former Nets guardDeron Williams after Brooklyn completed a buyout with the three-time All-Star point guard. Lin settled for a two-year, $4.3 million deal with the Charlotte Hornets, which included a second-year player option.

“I could count so many players that had a higher market value and a higher contract. It was just like, ‘Wow,'” said Lin, who was the 246th-highest paid NBA player last season, according to ESPN contract data. “I didn’t realize that my value had dropped so much during my one season with the Lakers.”

In Charlotte, however, Lin flourished. Empowered by a defined role (sparkplug reserve) and a coach (Steve Clifford) who believed in him, Lin revitalized his career and rediscovered his passion for basketball, helping the Hornets make the playoffs.

“If you look at the season with the Hornets and my season with the Lakers, statistically, they’re pretty much the same,” Lin said. “And maybe it’s perception or whatever happened with the Lakers, and the fact that we were getting blown out on national TV consistently — I don’t know what it was. Was it fair? Do I think I’m worth more than that? Yeah, I think I’m worth more than the veteran’s minimum.”

Lin proved as much with the Hornets. His value had been restored.

The Kenny Atkinson factor

Atkinson won’t take credit for the Nets landing Lin. But the truth is, he should. After all, Lin has admitted he wasn’t really considering Brooklyn until Atkinson came aboard.

Atkinson and Lin go back to their days in New York when Lin wasn’t known yet and Atkinson was an assistant trying to develop end-of-the-bench players.

Early on, Atkinson wondered, “Is this kid any good?” Especially after Lin got torched in a 3-on-3 practice game by another assistant coach while the Knicks were on the road. But the two kept after it, their bond growing stronger with every workout.

It all paid off as Lin went from nearly being released to captivating the world, causing Atkinson to become emotional with each big shot Lin hit.

The offseason after “Linsanity,” Atkinson and Lin reconvened in Palo Alto for nearly a month to work out. During that time, Atkinson — who brought his family with him to the West Coast — learned of Lin’s backstory: from underdog high school state champion to Harvard to undrafted D-Leaguer. They went to the local mall and ate the same deli sandwiches Lin had enjoyed as a kid.

“I really got to know him and started to appreciate him more,” Atkinson said. “It was really a friendship. Your coach is usually your mentor, but we’d go out and mix it up a bit, playing a lot of one-on-one during my younger days. I think he liked my energy.”

But their paths diverged.

Lin was a restricted free agent and assumed he’d be back with New York. Rivals weren’t clamoring for his services, figuring it was a formality that the Knicks were going to match. But they never even gave him an offer. The Houston Rockets gave him an offer sheet the Knicks couldn’t reasonably match, and that was that. Atkinson, meanwhile, headed to Atlanta to become an assistant coach for the Hawks.

“I was open to it,” Lin said of a reunion with New York. He even prayed about returning to the Big Apple. “But the Knicks haven’t really shown an interest the last few years, and that’s fine. There are no hard feelings, but I personally would’ve been open to it.”

Lin is playing on the other side of the Brooklyn Bridge now, and Nets fans can thank Atkinson, who pushed extremely hard to be able to reunite with his old project player.

Why do the two mesh so well?

“His dedication, his focus, his stubbornness,” Atkinson replied. “His love and passion for the game, and him always playing with a chip on his shoulder.”

Being a top priority

After a season in which Jarrett Jack (before his season-ending ACL injury), Larkin and Donald Sloan split duties at point guard, the rebuilding Nets — under new GM Sean Marks — identified point guard as their top need and Lin as their top target.

Lin took three weeks to clear his mind after the Hornets were eliminated from the postseason, and then met with his co-agents, Jim Tanner and Roger Montgomery, before departing for a three-week trip to Asia.

Re-signing with Charlotte was not an option because the Hornets did not own Lin’s Bird rights — which prohibited them from exceeding the salary cap to sign him — and needed to give raises to starters Nicolas Batum and Marvin Williamsin order to keep them.

The Rockets inquired about his services, sources said. They had recently hired offensive mastermind Mike D’Antoni — Lin’s coach with the Knicks — but talks with Lin never advanced.

In Houston, Lin struggled to mesh with James Harden. Then-Rockets coachKevin McHale ripped Lin in the playoffs. His two-year tenure ended when he was traded to L.A., and things certainly didn’t improve alongside then-Lakers coach Byron Scott and Kobe Bryant. After Lin’s season with the Lakers, he told The New Yorker he gained 21 pounds in 21 days.

Yet, as July 1, 2016 neared, after one season in Charlotte, Lin was drawing interest from other teams again. He said being a wanted man in free agency was “stressful, but a good stressful.”

During the Nets’ meeting with Lin, which included Marks, Atkinson, other front-office people and franchise center Brook Lopez, the Nets made a compelling pitch featuring their ever-evolving system and culture.

“We were showing the positives of playing out here,” Lopez said. “Obviously, he knows that firsthand from playing with the Knicks, but just how we can benefit from him and how he can benefit from us — that sort of symbiotic relationship. We just kept harping on the differences in our organization and the franchise as a whole from the top down.”

Marks and Atkinson did most of the talking, Lopez said, but the Nets left unsure of where they stood, according to sources.

Lin was highly intrigued about the possibility of teaming up with Pelicans starAnthony Davis and playing for Alvin Gentry in New Orleans’ D’Antoni-inspired offense, sources said. But the Pelicans weren’t willing to pay what the Nets were, paving the way for Lin’s return to New York.

Brooklyn also made sure Lin knew he was their top priority, and they treated him that way, sources said. Lopez said Lin texted the center before making the move public.

“It’s so huge for me, just having a guy who’s such a downhill driver,” said Lopez, the other half of Brook-Lin’s pick-and-roll tandem. “He draws so much attention that it makes it easier for everyone else on the floor since he’s such a willing passer.”

Nowhere to go but up?

With the Nets, the word “playoffs” has been replaced by “progress.”

Even internally, members of the organization wonder about the lack of talent on the roster.

Brooklyn gave restricted free agents Allen Crabbe and Tyler Johnson massive offer sheets, but Portland and Miami matched.

But Lin, already having endured the extremes of an up-and-down career, has what he wants — the keys to a franchise and significant responsibility — along with the backing of his die-hard fans, who point out that statistics show he performs better as a starter than as a reserve.

This is the 28-year-old veteran’s chance to change the perception that he isn’t just a three-week wonder.

“I’m just embracing it,” Lin said. “There are days where I come back from practice and the only thing I do is I just start thanking God for this opportunity. It just feels good to be a leader, because when you say something there’s a different level of respect and guys look to you to lead.

“That’s kind of what I’ve been my whole life, is the starting point guard and the leader of the team, and for me to be back in that position, I’m just like, ‘Man, this is really cool.’ And it feels natural again. The last three years, I’ve kind of been a backup or lost in the mix. Here, everything just seems so much more clear.”