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.
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.
The syntax of the method is as follows:
List.reversed
Here, List
is the original input list to the function that will be reversed.
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);}
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.
The time complexity of the algorithm is
The space complexity of the algorithm is
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
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);}
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
Line 16: To complete the swap, we add the stored value in temp
to the
The time complexity of the algorithm is
The space complexity of the algorithm is
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.
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);}
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 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
The time complexity of the algorithm is
The space complexity of the algorithm is
Free Resources