C++ List Reverse

In the C++ programming language, the list container in the Standard Template Library (STL) offers a very flexible and efficient way to manage sequences of elements. The reverse function is a member function of the std::list container. The list::reverse function is commonly utilized to reverse the order of elements in the list in place. It effectively rearranges the underlying sequence of the list so that the first element becomes last, and the last element becomes first.

In C++ , the std::list::reverse function is particularly useful when we need to quickly invert the sequence of stored elements without creating a new container.

Syntax:

It has the following syntax:

Example

list_name.reverse();

In this syntax,

  • list_name: It represents the name of the list in C++.
  • reverse: This function is used to reverse the elements.
  • Return value: It does not return any value.

Time complexity:

It has O(n) time complexity, where n is the number of elements in the list.

C++ Simple std::list::reverse function Example

Let us take a simple example to illustrate the std::list::reverse function in C++.

Example

Example

#include <iostream>  

#include <list>  

using namespace std;     //using standard namespace

int main ()     //main function

{  

   std::list <int> li={1,2,3,4,5,6};  

    cout<<"Content of list li is :";  

    for (list <int> :: iterator itr=li.begin ();itr!=li.end ();++itr)  

    cout <<*itr;  

    li.reverse ();  

    cout <<'\n';  

    cout <<"After reversing, the content of the list li is :";  

    for (list <int> :: iterator itr=li.begin ();itr!=li.end ();++itr)  

    cout <<*itr;  

    cout <<'\n';  

    return 0;  

}

Output:

Output

Content of list li is : 123456

After reversing, the content of the list li is : 654321

Explanation:

In this example, we create a list of integers and display its original content. After that, we use the reverse function to reverse the order of the elements, and print the updated list, which shows how the sequence gets inverted in place.

C++ std::list::reverse Function Example to Reverse a List of Strings

Let us take a simple example to illustrate the std::list::reverse function when the list contains strings.

Example

Example

#include <iostream>  

#include <list>  

using namespace std;  

int main ()  

{  

   std::list<string> li={"mango", "is", "a", "fruit"};  

    cout <<"Content of list li is :";  

    for (list<string> :: iterator itr=li.begin ();itr!=li.end ();++itr)  

    cout <<*itr<<" ";  

    li.reverse();  

    cout <<'\n';  

    cout  <<"After reversing, the content of the list li is :";  

    for (list<string> :: iterator itr=li.begin ();itr!=li.end ();++itr)  

    cout <<*itr<<" ";  

    cout <<'\n';  

    return 0;  

}

Output:

Output

Content of list li is : mango is a fruit

After reversing, the content of the list li is : fruit a is mango

Explanation:

In this example, we have created a list of strings, and its contents are displayed in the original order. After that, we use the reverse function, which reverses the sequence of elements in place, and the updated list is printed to show the reversed order.

Key Features of the std::list::reverse Function

There are several features of the std::list::reverse function in C++. Some of them are as follows:

  • It does not create a new list. It makes changes to the existing one.
  • It simply swaps the next and prev pointers of nodes.
  • It has taken only O(n) time complexity.
  • It doesn't need any extra parameters.
  • Drawbacks of std::list::reverse function

There are several drawbacks of the std::list::reverse function in C++. Some of them are as follows:

  • The function is only possible with std::list. It does not work with other STL containers.
  • It cannot reverse only a part of the list. If we only want to reverse a portion, we can use the std::reverse function with iterators.
  • It is very useful for lists, but it needs O(n) time, which can be expensive for large lists.
  • Conclusion

In conclusion, the list::reverse function in C++ is a very efficient way of reversing the order of elements in a std::list. Since std::list is implemented as a doubly linked list, reversing the contents of a std::list is done by rearranging pointers instead of copying or reallocating data, which means it utilizes little memory overhead and is straightforward in execution. This function is very useful to a programmer for real-world cases, such as task scheduling, undo and redo functionality, or reverse traversal of algorithms.

Std::list::reverse Function FAQs

1) What does the list::reverse function do in C++?

The list::reverse is used to reverse the order of all of a std::list's elements, such that the front of the list becomes the back of the list, and the back of the list becomes the front. It does this by rearranging the node pointers; it doesn't copy or reallocate existing data. The cost for this operation is O(n).

2) Does the list::reverse function create additional memory?

No, the list::reverse function only modifies the existing node pointers when reversing the list, which means that it does not allocate any extra memory for a new linked list. It simply updates the next and prev pointers existing in the list.

3) Can we reverse part of a list using list::reverse in C++?

No, list::reverse always reverses the entire list. If we want to reverse only part of the list, we can use the std::reverse from <algorithm>, which gives us an iterator-based range with greater control of the area to reverse.

4) What is the time complexity of list::reverse in C++?

The time complexity of list::reverse is linear, O(n), because the function has to visit each node once. Yet it is efficient because only the pointers are swapped as various nodes are traversed.

5) When should we use list::reverse instead of std::reverse in C++?

We would use list::reverse only when working with std::list because it is optimized for linked lists. For other containers like vector, deque, or arrays, we would use std::reverse because they use contiguous memory.

Input Required

This code uses input(). Please provide values below: