How do I redirect a domain but keep the URL structure?
Your second variant is close to the goal. But it needs some edits. Firstly, you should escape all dots in RewriteCond, so it will be
RewriteCond %{HTTP_HOST} ^example\.co\.uk$ [NC]

Secondly you need to edit RewriteRule to change regular expression and append request uri:
RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]

It also matters where you place the code. Your second variant is right. It's best to place code just after RewriteEngine on
Final code will be:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.co\.uk$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]