List reverse function in C++ STL

In this article we will be discussing the working, syntax and examples of list::reverse() function in C++.

What is a List in STL

List is a data structure that allows constant time insertion and deletion anywhere in sequence. Lists are implemented as doubly linked lists. Lists allow non-contiguous memory allocation. List perform better insertion extraction and moving of element in any position in container than array, vector and deque. In List the direct access to the element is slow and list is similar to forward_list, but forward list objects are single linked lists and they can only be iterated forwards.

What is list::reverse()

list::reverse() is an inbuilt function in C++ STL which is declared in header file. reverse() is used to reverse the list container, means the last element of the list becomes the first element of the list.

Below is the graphical representation of the list and and its reversed form −



This function doesn’t accepts any parameter.

Return Value

This function returns nothing. It will only reverse the container, whose list is to be reversed.


 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   //create a list
   list<int> myList;
   //inserting elements to the list
   //list before appyling reverse() function
   cout<<"List : ";
   for (auto i = myList.begin(); i != myList.end(); i++)
      cout << *i << " ";
   //reversing the list
   cout<<"\nList after appyling reverse() : ";
   for (auto i = myList.begin(); i != myList.end(); i++)
      cout << *i << " ";
   return 0;


If we run the above code then it will generate the following output

List : 1 2 3 4
List after applying reverse(): 4 3 2 1

Updated on: 26-Feb-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started