std::forward_list::reverse() is an inbuilt function in CPP STL which reverses the order of the elements present in the forward_list.
Syntax:
forwardlist_name.reverse()
Parameter: The function does not accept any parameter.
Return value: The function has no return value. It reverses the forward list.
Below program demonstrates the above function:
Program 1:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
forward_list< int > forward = { 10, 20, 40, 30, 70 };
cout << "List elements before performing reverse operation: " ;
for ( auto it = forward.begin(); it != forward.end(); ++it)
cout << *it << " " ;
forward.reverse();
cout << "\nList elements after performing reverse operation: " ;
for ( auto it = forward.begin(); it != forward.end(); ++it)
cout << *it << " " ;
return 0;
}
|
Output
List elements before performing reverse operation: 10 20 40 30 70
List elements after performing reverse operation: 70 30 40 20 10
Time Complexity: O(n)
Auxiliary Space: O(n)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!