The Dow and S&P 500 finished at new 5-year highs Tuesday, as investors grew hopeful about a potential merger between OfficeMax and Office Depot.

The Dow Jones industrial average rose 0.4%, while the S&P 500 added 0.7%. The Nasdaq gained 0.7% to close at its highest level since November 2000.

Investors have been pulling back a bit after a strong start to the year. All three indexes are up between 6% and 8%, with the Dow just 1.2% shy of its all-time high, hit in October 2007. The S&P 500 is less than 3% below its record high, also set in October 2007.

Driving the gains Tuesday were shares of OfficeMax (OMX) and Office Depot (ODP), which both rallied after media reports said the companies were in advanced merger talks, citing unnamed sources. Staples (SPLS), the other major office supplies retailer, gained about 13%.

The talks come as mergers and acquisitions have picked up. Just last week, US Airways (LCC) announced a merger with American Airlines parent AMR (AAMRQ); Warren Buffett's Berkshire Hathaway (BRKA) announced it was buying ketchup maker Heinz (HNZ); and Comcast (CMCSA) announced a $16.7 billion deal for the 49% of NBC Universal that was still owned by General Electric (GE).

Best Buy (BBY) shares gained almost 3% after the electronics retailer announced plans to make its price-matching program permanent on March 3.

Related: Google is the new Apple..for now

Shares of Google (GOOG) rose to a new all-time high of $804 per share.

After the closing bell, Dell (DELL) reported earnings and revenue that fell from a year ago but topped analyst expectations. Controversial nutritional supplements company Herbalife (HLF) also announced better-than-expected earnings and sales figures. The company also boosted its outlook for 2013 and issued a 30 cents per share quarterly dividend.

A hearing began Tuesday in David Einhorn's lawsuit against Apple (AAPL). Einhorn's hedge fund, Greenlight Capital, has accused Apple of hoarding billions in cash and wants a judge to block a "bundled" shareholder vote, scheduled for Feb. 27. Einhorn wants shareholders to vote separately on an Apple proposal that would limit the company's ability to issue high-yielding preferred stock.

Apple CEO Tim Cook has called the lawsuit a "silly sideshow."

Tuesday's homebuilders confidence report did little to soothe investor's nerves about the wobbly economic recovery. The National Association of Home Builders' Housing Market index fell for the first time in nearly a year in February. Economists were expecting the index to edge up to a six-year high.

Separate reports on housing starts and building permits will follow on Wednesday, and data on existing home sales will be released Thursday.

Related: Fear & Greed Index firmly in extreme greed

European markets finished with solid gains, lifted by a stronger-than-expected reading from the latest ZEW survey of German investor sentiment.

Asian markets closed lower. The Shanghai Composite lost 1.6%, the Hang Seng declined 1% and the Nikkei dropped 0.3%.

Check gas prices in your state

Gas prices rose for the 33rd day in a row. Economists are monitoring rising gas prices to gauge whether they will take a bite out of consumer spending in February.

Nationwide, a gallon of unleaded gasoline averaged $3.748 Tuesday, according to AAA. Gas prices have risen about 33 cents, or nearly 10%, since the beginning of February.

Oil prices edged higher while gold prices declined.

The dollar lost ground versus the euro and the Japanese yen but rose versus the British pound.

The price on the 10-year Treasury fell slightly, pushing the yield up to 2.03% from 2% late Friday.