How to reverse a list in Dart

A list in Dart is an ordered collection of data objects. Multiple ways to manipulate it include sorting, merging, or reversing.

Reversing a list of length nn refers to switching the elements on the 0th0^{th} and n1thn-1^{th} indexes, the 1st1^{st} and n2thn-2^{th} indexes, and so on.

Therefore, given the following list:

Reversing it returns the following:

The example above is represented in the illustration below:

Reversing a list

There are three methods to reverse a list in Dart:

  • Using an in-built method.

  • Using a loop to reverse in-place.

  • Using a loop to create a new list.

Using an in-built method

The List.reversed method in Dart returns an iterable object with the list elements in reverse order. The returned object can be used to create a new list.

Syntax

The syntax of the method is as follows:

List.reversed

Here, List is the original input list to the function that will be reversed.

Example

The following code demonstrates how to reverse a list using List.reversed method:

void main(){
// creating a list
var orgList = [1, 2, 3, 4, 5];
print(orgList);
// reversing the list
var objList = orgList.reversed ;
// creating a new list with the object
var revList = new List.from(objList);
print(revList);
}

Explanation

The following explains the code:

  • Line 3: We initialize a list named orgList.

  • Line 7: We reverse orgList using the in-built method. The result is an iterable object, which we store in the variable objList.

  • Line 10: We use the new List.from() constructor to create a new list by passing the object with the reversed elements to it.

Time complexity

The time complexity of the algorithm is O(n)O(n) where nn is the number of elements in the list.

Space complexity

The space complexity of the algorithm is O(n)O(n) as we create a new list.

Using a loop to reverse in-place

An in-place algorithm results in the reversed list occupying the same storage as the original list. In this method, we iterate half of the list using a for loop and swap the elements on the ithi^{th} index with the elements on the n1ithn-1-i^{th} index.

Example

The following code demonstrates how to reverse a list in-place using a for loop:

void main(){
// creating a list
var orgList = [1, 2, 3, 4, 5];
print(orgList) ;
// the main loop to reverse the list
// the loop iterates for half of the list
for (var i = 0 ; i < orgList.length/2 ; i++)
{
// store the current index in a temp variable
var temp = orgList[i];
// add the value from the corresponding index
// from the end to the current index
orgList[i] = orgList[orgList.length-1-i];
// add the temp value to the ending index
orgList[orgList.length-1-i] = temp;
}
print(orgList);
}

Explanation

The following explains the code:

  • Line 3: We initialize a list named orgList.

  • Lines 8–17: We iterate from the first element to the middle of the list.

    • Line 11: We store the current index in a temporary variable, temp.

    • Line 14: We assign the value of the length1ithlength-1-i^{th} index to the current index. For instance, if the current index is 11 and the length of the list is 55, the value from the index 511=35-1-1=3 will be added.

    • Line 16: To complete the swap, we add the stored value in temp to the length1ithlength-1-i^{th} index.

Time complexity

The time complexity of the algorithm is O(n)O(n) where nn is the number of elements in the list.

Space complexity

The space complexity of the algorithm is O(1)O(1) as the reversal is in-place.

Using a loop to create a new list

In this method, the original list is added to a new list using a reversed for loop. Here, the last element of the original list will be added to the first index of the new list, and so on.

Example

The following code demonstrates how to reverse a list by adding to a new list using a for loop:

void main(){
// creating a new list
var orgList = [1, 2, 3, 4, 5];
print(orgList) ;
// creating another list of the same length
var revList = new List(orgList.length);
// counter will be used to index the reversed list
var counter = 0 ;
// the main loop
for (var i = orgList.length-1 ; i >= 0 ; i--)
{
// assign the element to the reversed list
revList[counter] = orgList[i];
// increment the counter
counter++ ;
}
print(revList);
}

Explanation

The following explains the code:

  • Line 3: We initialize a list named orgList.

  • Line 7: We use the new List() constructor and orgList.length method to create a new list of the same length named revList.

  • Line 10: We initialize a counter with 00 to keep track of the index of revList.

  • Lines 13–19: We iterate from the last index till the start of the list.

    • Line 16: We assign the element present in the current index of the orgList to the counter index of the revList.

    • Line 18: We increment counter by 11.

Time complexity

The time complexity of the algorithm is O(n)O(n) where nn is the number of elements in the list.

Space complexity

The space complexity of the algorithm is O(n)O(n) as we create a new list.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved