Every head on every figure is different. There is no one answer to this question. If you’re lucky, the head and body you are matching up will be compatible, and all you have to do is make the swap. But that’s unlikely.
Swivel head joints are tend to be easier. If I need to make a swivel had joint from scratch I’ll use a bit of styrene rod to make a peg, and drill and matching hole. (See the photo below.) If the new peg isn’t quite snug enough I can expand it’s radius by wrapping tape around it.
Alternatively… Magnets! (Magnets make great swivel joints.)
Ball-and-socket joints can be messier. There’s often a “dumbell” shaped piece that links the head and neck, or at least a peg with a ball on it. Best case scenario, the swap magically works. Sometimes the fit is there, but the neck is too short or shaped wrong. In those cases, you’ve got to apply your sculpting medium of choice to build up the neck. Maybe the head and peg fit, but the head sits too high. Or the head and peg don’t fit at all. In those cases, you have to get creative. There is no one solution. You might have to remove the “dumbell,” if you can, and blast away the neck. Then re-insert the “dumbell” and rebuild the neck. In some cases the ball-and socket functionality can’t be saved. Then I just resort to making a swivel joint, as outlined above. Again, every scenario is different.