sounds like scaitica. the main large nerve running down your legs is the sciatic nerve but it leads to the spine and right past your gynaecological bits so if you have endo or an ovarian cyst or even adhesions that pull or press on the sciatic nerve in certain positions in can cause pain reactions exactly as you describe then altering your position relieves the pressure and thus the pain you feel.
Please visit your GP about this, if it is caused by something fixable like a cysts which can be removed or adhesionscut back or endo cut away then problem is solved. But it needs investigating.
You may find it easier in the meantime to sleep in a different position ie sitting up. Relocate to the sofa to sleep a better pain free sleep,or get a bolster cushion to elevate your leg while you sleep or try putting a decent depth of cushion between your legs if you sleep on your side - as pregnant women do to take the pressure of their sciatic nerves which are being squished by the growing pregnancy bump.
Good Luck finding painless ways to sleep and hopefully getting to the root cause of this pain too.