“Lin-sanity” is coming back to New York City.

Jeremy Lin, the former Knicks guard who went from unheralded bench player to NBA darling four years ago, is joining the Brooklyn Nets.

Lin confirmed the signing on Twitter, tweeting a photo of himself in a Nets jersey in front of a Nets logo with the caption “The journey continues...thankful for the next chapter!! #Godisgood.”

The deal is for three years and $36 million, according to Yahoo! Sports and ESPN. The deal includes a player option on the third year, trade kicker and bonuses, Yahoo! Sports reported. Lin can’t officially sign the deal until the NBA moratorium ends at 12:01 a.m. July 7.

Lin became an international sensation during the 2011-12 season for the Knicks, scoring the most points (136) by a NBA player in his first five starts since the 1976-77 merger. From April 2011 to April 2012, his replica jersey ranked second to the Bulls’ Derrick Rose -- and ahead of Kobe Bryant and LeBron James -- in sales at the NBA Store in Manhattan and on NBAShop.com.

Lin signed with the Rockets that following offseason for three years and $25.1 million, including $14.9 million in the third year. He played for them for two seasons then was traded to the Lakers in the third year of his contract. He spent last season with the Hornets.

Lin has averaged 11.7 points, 4.4 assists, 2.8 rebounds, 1.2 steals and 2.4 turnovers per game since signing with the Warriors an an undrafted free agent out of Harvard before the 2010-11 season.