This article explores different ways to get a reverse copy of a list in Kotlin. The specified list may or may not be mutable, but the copy of the specified list must be mutable.

1. Using reversed() function

The standard solution is to call the reversed() function, which returns a list with reversed order elements.

Download Code

 
To get a mutable list, you can call the toMutableList() function:

Download Code

2. Using asReversed() function

If you just need the view of the list in reverse order, consider using the asReversed() function.

Download Code

 
To get a new mutable list instance, wrap the returned view under the ArrayList constructor:

Download Code

3. Using map() function

Here, the idea is to get all valid indices of the list and map each index to its corresponding value in the reversed list.

Download Code

 
You can also collect the elements in an ArrayList.

Download Code

4. Using ListIterator

Another solution is to use a special iterator, ListIterator, which offers bidirectional access. The idea is to add the original list elements to a new list instance by iterating the list in reverse order.

Download Code

5. Using for loop

Finally, you can also iterate the List in reverse order using a for-loop, as shown below:

Download Code

 
This can also be done using the forEach() function that performs the given action on each list element.

Download Code

That’s all about getting a reverse copy of a list in Kotlin.