Get a reverse copy of a list in Kotlin
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.
|
1 2 3 4 5 6 |
fun main() { val list = (1..5).toList() val reverse: List<Int> = list.reversed(); println(reverse) } |
To get a mutable list, you can call the toMutableList() function:
|
1 2 3 4 5 6 |
fun main() { val list = (1..5).toList() val reverse: MutableList<Int> = list.reversed().toMutableList(); println(reverse) } |
2. Using asReversed() function
If you just need the view of the list in reverse order, consider using the asReversed() function.
|
1 2 3 4 5 6 |
fun main() { val list = (1..5).toList() val reversedView: List<Int> = list.asReversed(); println(reversedView) } |
To get a new mutable list instance, wrap the returned view under the ArrayList constructor:
|
1 2 3 4 5 6 |
fun main() { val list = (1..5).toList() val reversedView: MutableList<Int> = ArrayList(list.asReversed()); println(reversedView) } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 |
fun <T> reverseList(list: List<T>): List<T> { return list.indices.map { i: Int -> list[list.size - 1 - i] } } fun main() { val list = (1..5).toMutableList() val reverse = reverseList(list) println(reverse) // [5, 4, 3, 2, 1] } |
You can also collect the elements in an ArrayList.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
fun <T> reverseList(list: List<T>): ArrayList<T> { return (list.indices) .map { i: Int -> list[list.size - 1 - i] } .toCollection(ArrayList()) } fun main() { val list = (1..5).toMutableList() val reverse = reverseList(list) println(reverse) // [5, 4, 3, 2, 1] } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
fun <T> reverseList(list: List<T>): MutableList<T> { val reverse: MutableList<T> = ArrayList(list.size) val itr = list.listIterator(list.size) while (itr.hasPrevious()) { reverse.add(itr.previous()) } return reverse } fun main() { val list = (1..5).toMutableList() val reverse: MutableList<Int> = reverseList(list) println(reverse) // [5, 4, 3, 2, 1] } |
5. Using for loop
Finally, you can also iterate the List in reverse order using a for-loop, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
fun <T> reverseList(list: List<T>): MutableList<T> { val reverse: MutableList<T> = ArrayList(list.size) for (i in list.indices.reversed()) { reverse.add(list[i]) } return reverse } fun main() { val list = (1..5).toMutableList() val reverse: MutableList<Int> = reverseList(list) println(reverse) // [5, 4, 3, 2, 1] } |
This can also be done using the forEach() function that performs the given action on each list element.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
fun <T> reverseList(list: List<T>): MutableList<T> { val reverse: MutableList<T> = ArrayList(list.size) list.indices.reversed().forEach { reverse.add(list[it]) } return reverse } fun main() { val list = (1..5).toMutableList() val reverse: MutableList<Int> = reverseList(list) println(reverse) // [5, 4, 3, 2, 1] } |
That’s all about getting a reverse copy of a list in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)