Reverse a linked list using C++
Aug 27, 2023 pm 12:09 PMIn this article, we need to reverse the link with the help of a singly linked list. Our task is to create a function that can reverse a given singly linked list. For example
1 2 3 4 5 6 7 |
|
Methods to find solution
There are different ways to reverse a linked list. Usually, we think of a simple way of reversing the linked list while traversing it.
Simple Method
In this method, we will traverse the linked list and try to reverse it during the traversal.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
Output
1 2 |
|
In this approach, we just iterate through the list and reverse it during the iteration. This is a good approach because the time complexity is O(N), where N is the size of our list.
Now we try to do an experiment and try to reverse the list using the stack.
Methods using stack
We will use a stack to store all the nodes in this program and reverse them by traversing the stack.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
|
Output
1 2 |
|
Explanation of the above code
In this method we store the list nodes in the stack while traversing the list , then use the stack to pop them and reverse the list; the time complexity of this approach is also O(N), where N is our list size. As before, we used the stack, so we can also use the recursive method, because recursion also uses the stack, and now we will use the recursive method.
Recursive Method
In this method we will perform the same process as before but using recursive calls.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
Output
1 2 |
|
In this method we are doing the same as before but using recursive calls so the time complexity of this method is also O(N), where N is the size of our list.
Conclusion
In this article, we solved the problem of inverting a singly linked list. We also learned the C program and the complete method (normal method and two other methods) to solve this problem. We can write the same program in other languages like C, Java, Python and others. Hope you find this article helpful.
The above is the detailed content of Reverse a linked list using C++. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Use C++ to write code to find the Nth non-square number

In C programming, find the area of a circle

Find the number of unique pairs in an array using C++

Inversion algorithm for right rotation of array written in C++

Write a code using C++ to find the number of subarrays with the same minimum and maximum values

Reverse doubly linked list grouping by given size using C++

Reversal algorithm for array rotation written in C++

Written in C++, find the number of reflexive relations on a set
