Stocks gained on optimistic prospects for a Greek bailout and despite several economic reports confirming a slowdown in the U.S. economy.
The Dow Jones Industrial Average gained more than 50 points after rising more than 120 earlier, and ending lower for fourth straight week on Friday.
Most Dow components gained, with Intel among the lead performers. The chip-maker unveiled a new category of laptops its calling Ultrabooks at a trade show in Taiwan.
TheS&P 500 and the Nasdaq also rose about a half a percent. The CBOE Volatility Index, widely considered the best gauge of fear in the market, rose above 16.
All key S&P 500 sectors gained, led by health care, technology and energy.
The brighter performance for stocks on the last day of the month is unlikely to turn around the market's negative performance in May. As of Friday, the Dow was down nearly 3 percent, the S&P 500 was down 2.4 percent, and the Nasdaq was down 2.7 percent. All major indices remain higher for the year, however.
If history is a guide, stocks won't turn around in June. The Dow has fallen for the last six Junes, and has been up only twice since 2000. Overall, the market has been down in June in the last four years.
Tuesday's performance owes much to a decision by some officials in Germany to allow Greece to restructure its debt, the Wall Street Journal reported over the weekend. That news encouraged investors concerned about Greece's future.
Investors pushed the euro to a three-week high against the dollar on the news, and oil prices also gained. U.S. Light Sweet Crude rose above $102 a barrel, while in London, Brent crude rose above $116.
Precious metals also rose, as gold traded near a four-week high above $1,538 an ounce, while silver traded above $38 an ounce.
Nokia tumbled after the handset maker cut its sales outlookon Tuesday. The company said it expects net sales from its products and services to be far worse than initially projections.
And Goldman Sachs gained after JPMorgan upgraded the investment firm to "overweight," citing overreaction to a report by a U.S. Senate panel and prospects for future business. Goldman was also in the news on Tuesday after a Wall Street Journal report that the bank had given Libya a chance to be a big shareholder during the financial crisis.
In M&A news, Ashland jumped after news the company would pay about $3.2 billion for International Specialty Products.
And Massey Energy gained despite pressure from shareholdersto block the company's purchase by Alpha Natural Resources, saying the deal would allow the coal company's directors to avoid responsibility for the April 2010 mine explosion that killed 29 miners. The shareholders have asked a judge to block the acquisition.
Meanwhile, solar stocks surged after reports that Germany planned to close all of its nuclear reactorsby 2022. LDK Solar , GT Solar and and First Solarall gained.
And Anadarko Petroleum rose after Wells Fargo said it expects the stock to rise once the oil company is able to fill "pent-up demand." The brokerage raised its price target on the stock to about $100 a share from $84.
Apple gained after news CEO Steve Jobs would offer the keynote speech at the iPhone maker's annual worldwide developers conference on June 6. The company will be unveiling several software developments at the conference.
One economic report after another Tuesday pointed to weakness in the U.S. economy. The Conference Board's May reading on consumer confidencefell to a six-month low at 60.8 from 66.0 in April. Economists surveyed by Reuters had expected the index to rise to 66.5.
Also, the Chicago Purchasing Managers Index fell to 56.6 in May from 67.6 in April. Economists surveyed by Reuters had expected the index to fall to 62.6.
And prices for U.S. single-family homes sank to a new low for the year, providing clearer evidence of a double-dip in housing. The S&P/Case Shiller composite index of 20 metropolitan areas fell 0.2 percent in March from the month before, which was in line with expectations.
The 20-city composite index at 138.16 is now below the April 2009 low of 139.26 reached in April 2009.
Asian stocksgained on a more upbeat outlook from manufacturers in Japan.
In Europe, shares ended higher lifton expectations there would be a bailout package for Greece, and despite the fact German unemployment fell less than expected in May.
On Tap This Week:
WEDNESDAY: Mortgage applications, Challenger job-cut report, ADP employment report, ISM manufacturing index, construction spending, auto sales; D9: All Things Digital Conference.
THURSDAY: Chain store sales, jobless claims, factory orders, natural gas inventories, oil inventories, money supply.
FRIDAY: Non-farm payroll report, ISM non-manufacturing index; Wal-Mart shareholder meeting.
More on CNBC.com