The Messenger of Allah (sal Allahu alaihi wa sallam) said: “Beware, no one should spend a night alone in a house with a single (divorced or widowed) woman unless he is married to her, or happens to be her Mahram.” [Muslim]
“In the above Hadith, it is prohibited for any man to spend a night alone in a house with a non-Mahram woman. This prohibition is based on foresight and wisdom. In principle, it is prohibited for a non-Mahram man and woman to be alone together under all circumstances, but the specific prohibition of spending a night alone under one roof has been separately mentioned, because in the darkness of night where others are not likely to witness anything, the opportunities for misconduct are greater. Again, all the non-Mahram relatives, such as cousins and brothers-in-law, are also included in this prohibition. Often, women do not take precaution with these men and go in front of them without Hijab. This prohibition is both for men and women. Men have been addressed in the Hadith, because they are stronger and may not be easily deterred by a woman.
“Imam Nawawi writes in Sharah Muslim that the reason that divorced and widowed women were separately mentioned in this Hadith is that due to their being alone, these women become easy prey for men who are looking to marry or have bad intentions. They will not dare to visit single girls because they protect themselves and are also protected by their parents.” [Hijab by Dr. Mohammed Ismail Memon Madani]