0
0
KotlinProgramBeginner · 2 min read

Kotlin Program to Reverse List with Example and Explanation

In Kotlin, you can reverse a list by using the reversed() function like val reversedList = originalList.reversed() which returns a new list with elements in reverse order.
📋

Examples

Input[1, 2, 3, 4, 5]
Output[5, 4, 3, 2, 1]
Input["apple", "banana", "cherry"]
Output["cherry", "banana", "apple"]
Input[]
Output[]
🧠

How to Think About It

To reverse a list, think of taking the last element and making it first, then the second last element next, and so on until the first element becomes last. Kotlin provides a simple function reversed() that does this for you by creating a new list with elements in reverse order.
📐

Algorithm

1
Get the original list as input
2
Use the built-in <code>reversed()</code> function to create a new list with elements in reverse order
3
Return or print the reversed list
💻

Code

kotlin
fun main() {
    val originalList = listOf(1, 2, 3, 4, 5)
    val reversedList = originalList.reversed()
    println(reversedList)
}
Output
[5, 4, 3, 2, 1]
🔍

Dry Run

Let's trace the list [1, 2, 3, 4, 5] through the code

1

Original List

originalList = [1, 2, 3, 4, 5]

2

Call reversed()

reversedList = originalList.reversed()

3

Resulting List

reversedList = [5, 4, 3, 2, 1]

4

Print Output

Output: [5, 4, 3, 2, 1]

StepList State
1[1, 2, 3, 4, 5]
2[5, 4, 3, 2, 1]
💡

Why This Works

Step 1: Using reversed() Function

The reversed() function creates a new list with elements in the opposite order of the original list.

Step 2: Immutable Original List

The original list remains unchanged because reversed() returns a new list instead of modifying the original.

Step 3: Printing the Result

Printing the reversed list shows the elements from last to first, confirming the reversal.

🔄

Alternative Approaches

Using reversedArray() and converting to list
kotlin
fun main() {
    val originalList = listOf(1, 2, 3, 4, 5)
    val reversedList = originalList.toTypedArray().reversedArray().toList()
    println(reversedList)
}
This converts the list to an array, reverses it, then converts back to a list; slightly less efficient but useful if working with arrays.
Using a manual loop to reverse
kotlin
fun main() {
    val originalList = listOf(1, 2, 3, 4, 5)
    val reversedList = mutableListOf<Int>()
    for (i in originalList.size - 1 downTo 0) {
        reversedList.add(originalList[i])
    }
    println(reversedList)
}
This manually builds a reversed list by looping backward; more code but shows the reversal logic explicitly.

Complexity: O(n) time, O(n) space

Time Complexity

The reversed() function iterates over all elements once, so it takes linear time proportional to the list size.

Space Complexity

It creates a new list to hold the reversed elements, so it uses extra space proportional to the list size.

Which Approach is Fastest?

Using reversed() is the fastest and most readable. Manual loops are slower and more error-prone. Array conversion adds overhead.

ApproachTimeSpaceBest For
reversed()O(n)O(n)Simple and readable reversal
reversedArray() with conversionO(n)O(n)When working with arrays
Manual loopO(n)O(n)Learning reversal logic explicitly
💡
Use reversed() for a simple and readable way to reverse lists in Kotlin.
⚠️
Trying to reverse a list by modifying it directly without creating a new list, which is not possible with Kotlin's immutable lists.