Sen. Edward M. Kennedy discovered a possible answer to the presidential candidate's dilemma today, finding a way to turn the Iranian situation to his own political benefit without criticizing President Carter's handling of the crisis.

In a question-and-answer session with students at the University of Northern Iowa, Kennedy said the "spirit of unity" concerning the American hostages proves that Carter was wrong to say that the American people are suffering from "malaise." If the president had recognized that national spirit earlier, Kennedy said, he could have worked with Congress and the people to solve national problems.

"Mr. Carter believes there is a malaise among the American people," Kennedy said. ". . . But I think the American people are prepared to deal with the problems that we have. One of the clearest examples is the sense of national unity that exists right now with regard to the hostages over in Tehran.

"There is a spirit of unity which I feel and everybody in this room feels.Now if we can develop that with regards to that situation, who is to say we cannot do it in trying to come to grips with our economy or trying to fashion an energy policy that's going to be fair?

"There is no reason we couldn't do it today if we'd stop downplaying the spirit of the American people."

Those comments, similar to remarks Kennedy made at a fund-raiser in Washington Sunday, got a warm reception from a student audience that had seemed only mildly enthusiastic up to that point.

And they suggested that Kennedy may have found a way to deal with the issue that has overshadowed the campaigns of candidates in both parties for the past six weeks.

Kennedy, like all of Carter's challengers, has been frustrated politically by the Iranian situation, because it diminshes public interest in other issues, and because Carter's performance to date has restored the president's image as a leader.

That frustration was probably partly responsible for Kennedy's harsh verbal attack on the deposed shah of Iran two weeks ago, an outburst that brought stinging rebuttals from politicians and some editorial writers.

As a result Kennedy essentially dropped the subject of Iran from his stump speeches, resricting his comments to general assurances that he and all Americans are unified behind the president.

His new formulation allows him to combine that assurance of unity with reiteration of a basic campaign theme: that Carter has failed as a leader because he has misread the attitudes of Congress and the people.

The senator's remarks came near the end of an appearance that followed a recurring pattern. He read a prepared speech in flat, almost soporific tones, then came alive during the ensuing question-and-answer session.

Kennedy arrived here on his fourth trip to Iowa in six weeks just after a new Des Moines Register poll showed that Carter had made a striking gain among Iowans since the Iranian crisis began.

The new poll shows Kennedy and Carter tied, 40 to 40, among Democrats here, with 9 percent supporting California Gov. Edmund (G.) Jerry Brown Jr. and the rest undecided. In the previous Register poll, in August, Kennedy led Carter 49 to 26.

Kennedy came here today with filmmaker Charles Guggenheim to make commercials for television. Guggenheim, who made presidental campaign ads for the senator's brothers, John and Robert, has a standard technique. He films the candidate in conversations with average, undecided voters. The viewer is supposed to identify with the undecided voter in the ad, and if Kennedy can convince the person he's talking to, it's presumed that he will convince the TV viewer as well.

Reporters were not allowed to watch the filming, but some of the voters who met with Kennedy in the guggenheim format said afterward they had been convinced to support the senator.

Farmer Gary Crawford, who was filmed talking to Kennedy at a tractor dealer's showroom near here, told reporters that he began the session "very neutral" and ended up supporting Kennedy.

"He was excellent," Crawford said. "Today, you put a question on him, he came up with an answer."

Kennedy had scheduled another half day's campaigning here for Wednesday morning, but he changed his plans and flew back to Washington late tonight for Wednesday's Senate votes on federal aid to Chrysler Corp.