Back to problems

Reverse a list in-place

Algorithm · Microsoft · Easy

Write a function that flips the order of the elements stored in a list, so that the element currently at the front ends up at the back and vice versa. The change has to happen in place: your routine must mutate the very list object it is handed and then give that same object back to the caller. Building a separate list to hold the answer is not permitted, which rules out slicing tricks such as arr[::-1], the list() constructor, and the built-in reversed(). Only a fixed…

Checking your access…