C++ <list> - reverse() Function
The C++ list::reverse function is used to reverse the order of elements of the list container.
Syntax
void reverse();
void reverse() noexcept;
Parameters
No parameter is required.
Return Value
None.
Time Complexity
Linear i.e, Θ(n).
Example:
In the example below, the list::reverse function is used reverse the order of elements of the list called MyList.
#include <iostream> #include <list> using namespace std; int main (){ list<int> MyList{10, 20, 30, 40, 50}; list<int>::iterator it; cout<<"MyList contains: "; for(it = MyList.begin(); it != MyList.end(); it++) cout<<*it<<" "; //Reverse the order of all elements of the list MyList.reverse(); cout<<"\nMyList contains: "; for(it = MyList.begin(); it != MyList.end(); it++) cout<<*it<<" "; return 0; }
The output of the above code will be:
MyList contains: 10 20 30 40 50 MyList contains: 50 40 30 20 10
❮ C++ <list> Library