The ruler of Delhi during the third Battle of Panipat in 1761 when Ahmad Shah Abdali defeated the Marathas was Shah Alam II.
Option 1: Alamgir I - This is not the correct answer as Alamgir I ruled from 1658 to 1707, which is well before the Battle of Panipat in 1761.
Option 2: Muhammad Shah - This is not the correct answer as Muhammad Shah ruled from 1719 to 1748, which is also before the Battle of Panipat in 1761.
Option 3: Jahandar Shah - This is not the correct answer as Jahandar Shah ruled from 1712 to 1713, again well before the Battle of Panipat.
The correct answer is option 4: Shah Alam II, who ruled from 1759 to 1806 and was the ruler of Delhi during the third Battle of Panipat in 1761.