After two years of stalemate, there is guarded optimism among Western diplomats that a solution to the Namibia problem -- Africa's last and longest independence struggle -- may be achieved next year.

Western diplomats who have long followed attempts to achieve independence for the South African-controlled territory say the atmosphere for a settlement is more positive than at any time during years of tortuous negotiations.

The remarks, however, are hedged with caution and warnings that many pitfalls still lie ahead.

A Zimbabwean official said, "I think South Africa still has several tricks up its sleeve."

The talks could easily break down over right-wing opposition in both South Africa and Namibia over Western-proposed guidelines for an independence Constitution. The details of a cease-fire and deployment of a U.N. peace-keeping force, which South Africa opposes, are also potential hazards to hopes for implementing an agreement next year.

Nevertheless, there has been an unmistakable mood of optimism among the representatives of five Western nations -- the United States, Britain, France, West Germany and Canada -- during their current 10-nation, 11-day negotiating tour of Africa.

The diplomats were in a chipper mood at a press conference in Salisbury yesterday and today in Maputo, Mozambique, they reportedly had a friendly meeting with President Samora Machel. Machel particularly asked to meet the head of the U.S. delegation, Assistant Secretary of State Chester Crocker, whom he snubbed in April.

A Western diplomat involved in the talks said, "I think we are closer to reality than ever before," adding that remarks last week by South African Foreign Minister Roelof (Pik) Botha were "the most positive" he had heard in years.

Botha said there was a real chance of a settlement coming from the latest initiative and that a failure could strain South Africa's relations with Washington. He added that if the Southwest Africa People's Organization (SWAPO), the guerrilla group fighting for independence, won free and fair elections, that result would have to be accepted.

Just a few months ago he said South Africa would never allow an independent Namibia to be ruled by Soviet-backed SWAPO.

Botha's latest remarks, and the fact that South Africa apparently did not raise any major objections to the proposed constitutional guidelines presented by the Western five, lend credibility to the private claims of some South African officials that Pretoria has decided to reach an international settlement in Namibia.

Under Crocker's leadership, the Western nations have embarked on a new negotiating method for Namibia with South Africa and the "internal" parties it supports, on the one side, and SWAPO and its African backers on the other.

The talks have been broken up into separate phases on the constitution and the cease-fire, reminiscent of the successful method used by British Foreign Secretary Lord Carrington two years ago in ending the 15-year-long Zimbabwe independence struggle. Crocker, who said yesterday that "the negotiating process is back on the road," has limited this phase of the talks to the constitutional guidelines. "It is quite possible in a matter of weeks or a couple months that we would have this phase of the process near to conclusion," he said in Salisbury.

The second phase is likely to be more difficult but success in the first phase could build momentum and make it harder for any of the parties to back out.

The contact group members have not received any definitive replies to their proposals. They say they have come to canvass reactions and take back suggestions.

The African states and SWAPO are expected to remain publicly noncommittal and wait for some tangible sign from Pretoria that its cooperation is sincere.

One possibility is that South Africa is using the current talks to test the political waters at home.

Pretoria has maneuvered the internal parties it backs in Namibia out front, so it can now allow them to go ahead with an agreement if there is no significant opposition in South Africa.

If serious right-wing opposition emerges, Prime Minister P.W. Botha can always pull the rug out from under the talks by having the internal parties object to the terms.

By allowing the talks to proceed in a mood of guarded optimism, he can see whether he will face serious accusations of selling the 100,000 whites in Namibia down the river. At the same time he can balance this threat against the advantages of better relations with the United States, a likely outgrowth of a Namibia settlement.

The Reagan administration also has been cheered by what appears to be some improvement in its relations with Angola, which has been the target of South African attacks because it allows sanctuary to SWAPO guerrillas.

U.S. Secretary of State Alexander M. Haig Jr. held an unpublicized, 30-minute meeting with Angolan Foreign Minister Paolo Jorge in New York in September. The meeting went well and Jorge subsequently took a more positive attitude toward the West's Namibian plan than Washington had expected, reliable sources in Johannesburg said.

One reason for the improved relations is that the United States is no longer demanding withdrawal of 15,000 Cuban troops from Angola as the price for a settlement.

The Cuban presence in Angola "is not on the agenda," the senior American official said. "Angola is a separate issue," he added but he repeated the U.S. position that a withdrawal of Cuban forces would "facilitate" a Namibian settlement.