If it is just on idle it could be a vacuum leak. Check for whistling noise from under the hood. Check all rubber hoses and bellows on air intake. When you were told to check the head gasket they probably meant the valve cover gasket as this is also prone to leaks. A vacuum leak could have caused the O2 sensors to fail as it would make the engine to run rich and fowl the sensors.
Good luck and let us know how you get on.