The rear wheel pivot approach will set you back a bike's length.
The front wheel pivot will require some landing spot in front of you.
Now if you are balancing onto sharp edges and there is nothing around, your only option is a clean and sharp U-turn on the spot assisted by a brief pedal kick. In all these techniques, body weight shifting is key, with both brakes locked at all times except during the brief pedal kick.
Turning 180° on the front wheel
Practice on some marks
Maintain a trackstand with your front wheel pointing in the direction opposite the rotation you want to initiate, so that you can switch the handlebars later and pivot with your inertial moment.
Initiate a rotation with your torso, concentrate your weight over the stem, with your shoulders well over the handlebars. As you turn the bars in one swift move, follow the rotation with your torso until you can no longer twist your hips, you should end up leaning forward into a full extension.
As you lean forward, tilt the bike onto the front wheel and swing the frame with your legs to push the rotation further, re-adjusting the frame in line with the front wheel. With the rear wheel lifted, keep your weight well above the front hub so that you don't get dragged sideways or lose your balance during the rotation.
Friction will stop you from spinning fairly quickly, especially with low pressure in the front tyre. You can swing the frame past that point by twisting your hips to push the rear wheel further into the change of direction.
Click on any step below and use the scroll-wheel to move through the animation.
Controlling the rotation
You can stop the rotation at any time and with precision by shifting your weight over the back wheel again to stick the rear tyre down. Even with the brakes locked, the front wheel still rolls slightly when you tilt the bike, so make sure to take this into account, especially on narrow edges.
At the beginning, it helps to learn this endo variation at a slow pace instead of starting from a trackstand, but you will never get as much control because the bike inertia could destabilize you.
It is better if you can control the whole move through pure body language. Pushing the front pivot to a full turn is really difficult and requires precise balance as you spin, but it is a nice trick for demos. It does help to learn in a mild slope, going upward.
Pivot the first 180° up against the slope and finish with the rear wheel completing the turn down into the slope. Gravity helps you swing the rear wheel back down into the slope, compensating for your lack of rotational inertia after the first 180° swing.