Yes you can leave coconut oil in your hair overnight and actually it's recommended to let the coconut oil sit in your hair overnight with a shower cap placed over your hair for maximum results.
Then in the morning take off the shower cap and throw it away and wash the coconut oil out of your hair.