How do you fix an iPhone 7 that keeps freezing?

Since a few days ago, my iPhone has been freezing randomly and shutting down, and then restarting on its own. As soon as it restarts, the phone freezes (before I can put in the password). I can't reboot the phone because it freezes before I can hold the home and power button long enough to do so. My iPhone will keep on freezing and restarting on its own until it runs out of battery. After I recharge it, the phone will function a few hours normally, until it starts freezing again. The process then repeats. How do I stop this?

Just melt it

Head to your closest Apple Store and speak to the Genius Bar. Might just need a battery replacement.

This sounds like a hardware issue. Take it to your local Apple store or authorized repair place, and they can run diagnostics for you.

I'd restore the phone through iTunes.