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
Therefore, given the following list:
Reversing it returns the following:
The example above is represented in the illustration below:
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 listvar orgList = [1, 2, 3, 4, 5];print(orgList);// reversing the listvar objList = orgList.reversed ;// creating a new list with the objectvar 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
orgListusing the in-built method. The result is an iterable object, which we store in the variableobjList.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
Space complexity
The space complexity of the algorithm is
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
Example
The following code demonstrates how to reverse a list in-place using a for loop:
void main(){// creating a listvar orgList = [1, 2, 3, 4, 5];print(orgList) ;// the main loop to reverse the list// the loop iterates for half of the listfor (var i = 0 ; i < orgList.length/2 ; i++){// store the current index in a temp variablevar temp = orgList[i];// add the value from the corresponding index// from the end to the current indexorgList[i] = orgList[orgList.length-1-i];// add the temp value to the ending indexorgList[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
index to the current index. For instance, if the current index is and the length of the list is , the value from the index will be added. Line 16: To complete the swap, we add the stored value in
tempto theindex.
Time complexity
The time complexity of the algorithm is
Space complexity
The space complexity of the algorithm is
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 listvar orgList = [1, 2, 3, 4, 5];print(orgList) ;// creating another list of the same lengthvar revList = new List(orgList.length);// counter will be used to index the reversed listvar counter = 0 ;// the main loopfor (var i = orgList.length-1 ; i >= 0 ; i--){// assign the element to the reversed listrevList[counter] = orgList[i];// increment the countercounter++ ;}print(revList);}
Explanation
The following explains the code:
Line 3: We initialize a list named
orgList.Line 7: We use the
new List()constructor andorgList.lengthmethod to create a new list of the same length namedrevList.Line 10: We initialize a counter with
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
orgListto thecounterindex of therevList.Line 18: We increment
counterby.
Time complexity
The time complexity of the algorithm is
Space complexity
The space complexity of the algorithm is
Free Resources