list reverse function in C++ STL
The list::reverse() is a built-in function in C++ STL which is used to reverse a list container. It reverses the order of elements in the list container.
Syntax:
list_name.reverse()
Parameters: This function does not accept any parameters.
Return Value: This function does not return any value. It just reverses the order of elements in the list container with which it is used.
Below program illustrate the list::reverse() function in C++ STL:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
list< int > demoList;
demoList.push_back(10);
demoList.push_back(20);
demoList.push_back(30);
demoList.push_back(40);
cout << "Initial List: ";
for ( auto itr = demoList.begin(); itr != demoList.end(); itr++)
cout << *itr << " ";
demoList.reverse();
cout << "\n\nList after reversing: ";
for ( auto itr = demoList.begin(); itr != demoList.end(); itr++)
cout << *itr << " ";
return 0;
}
|
Output
Initial List: 10 20 30 40
List after reversing: 40 30 20 10
Time Complexity – Linear O(N)
Last Updated :
28 Jun, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...