Michael Flynn has resigned as national security adviser over his contact with Russian officials before US President Donald Trump took office.