Table of Contents
Methods to find solution
Simple Method
Example
Output
Methods using stack
Explanation of the above code
Recursive Method
Conclusion
Home Backend Development C++ Reverse a linked list using C++

Reverse a linked list using C++

Aug 27, 2023 pm 12:09 PM
c programming Reverse linked list Linked list operations

Reverse a linked list using C++

In 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

Input:

Following Linked list :

1->2->3->4->NULL

 

Output:

After processing of our function:

4->3->2->1->NULL

Copy after login

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

#include<bits/stdc++.h>

using namespace std;

struct Node {

   int data;

   struct Node* next;

   Node(int data) {

      this->data = data;

      next = NULL;

   }

};

struct LinkedList {

   Node* head;

   LinkedList() { head = NULL; }

   // Function to print linked list

   void reverse() {

      auto curr = head; // current pointer

      Node* prev = NULL; // previous pointer

      while(curr) {

         auto temp = curr -> next;

         curr -> next = prev;

         prev = curr;

         head = prev;

         curr = temp;

      }

   }

   void print() {

      struct Node* temp = head;

      while (temp != NULL) {

         cout << temp->data << " ";

         temp = temp->next;

      }

   }

   void push(int data) {

      Node* temp = new Node(data);

      temp->next = head;

      head = temp;

   }

};

int main() {

   LinkedList list;

   list.push(20);

   list.push(4);

   list.push(15);

   list.push(85);

   list.print();

   list.reverse();

   cout << "\n";

   list.print();

}

Copy after login

Output

1

2

85 15 4 20

20 4 15 85

Copy after login
Copy after login
Copy after login

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

#include<bits/stdc++.h>

using namespace std;

struct Node {

   int data;

   struct Node* next;

   Node(int data) {

      this->data = data;

      next = NULL;

   }

};

struct LinkedList {

   Node* head;

   LinkedList() { head = NULL; }

   // Function to print linked list

   void reverse() {

      auto curr = head; // current pointer

      Node* prev = NULL; // previous pointer

      stack<Node *> s;

      while(curr) {

         s.push(curr);

         curr = curr -> next;

      }

      prev = s.top();

      head = prev;

      s.pop();

      while(!s.empty()) {

         auto temp = s.top();

         s.pop();

         prev -> next = temp;

         prev = temp;

      }

      prev -> next = NULL;

   }

   void print() {

      struct Node* temp = head;

      while (temp != NULL) {

         cout << temp->data << " ";

         temp = temp->next;

      }

   }

   void push(int data) {

      Node* temp = new Node(data);

      temp->next = head;

      head = temp;

   }

};

int main() {

   LinkedList list;

   list.push(20);

   list.push(4);

   list.push(15);

   list.push(85);

   list.print();

   list.reverse();

   cout << "\n";

   list.print();

}

Copy after login

Output

1

2

85 15 4 20

20 4 15 85

Copy after login
Copy after login
Copy after login

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

#include<bits/stdc++.h>

using namespace std;

struct Node {

   int data;

   struct Node* next;

   Node(int data) {

      this->data = data;

      next = NULL;

      }

   };

   struct LinkedList {

      Node* head;

      LinkedList() { head = NULL; }

      // Function to print linked list

      void rreverse(Node *curr, Node *prev) {

         if(curr == NULL) {

            // prev -> next = curr;

            head = prev;

            return;

         }

         rreverse(curr -> next, curr);

         curr -> next = prev;

         prev -> next = NULL;

      }

 

      void reverse() {

         auto curr = head; // current pointer

         Node* prev = NULL; // previous pointer

         rreverse(curr -> next, curr);

      }

      void print() {

         struct Node* temp = head;

         while (temp != NULL) {

         cout << temp->data << " ";

         temp = temp->next;

      }

   }

   void push(int data) {

      Node* temp = new Node(data);

      temp->next = head;

      head = temp;

   }

};

int main() {

   LinkedList list;

   list.push(20);

   list.push(4);

   list.push(15);

   list.push(85);

   list.print();

   list.reverse();

   cout << "\n";

   list.print();

}

Copy after login

Output

1

2

85 15 4 20

20 4 15 85

Copy after login
Copy after login
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use C++ to write code to find the Nth non-square number Use C++ to write code to find the Nth non-square number Aug 30, 2023 pm 10:41 PM

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

In C programming, find the area of ​​a circle In C programming, find the area of ​​a circle Aug 25, 2023 pm 10:57 PM

In C programming, find the area of ​​a circle

Find the number of unique pairs in an array using C++ Find the number of unique pairs in an array using C++ Sep 07, 2023 am 11:53 AM

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

Inversion algorithm for right rotation of array written in C++ Inversion algorithm for right rotation of array written in C++ Sep 08, 2023 pm 08:17 PM

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 Write a code using C++ to find the number of subarrays with the same minimum and maximum values Aug 25, 2023 pm 11:33 PM

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++ Reverse doubly linked list grouping by given size using C++ Sep 04, 2023 am 09:49 AM

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

Reversal algorithm for array rotation written in C++ Reversal algorithm for array rotation written in C++ Aug 28, 2023 pm 11:13 PM

Reversal algorithm for array rotation written in C++

Written in C++, find the number of reflexive relations on a set Written in C++, find the number of reflexive relations on a set Aug 26, 2023 pm 08:17 PM

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

See all articles