C++ program to reverse a linked list by changing links between nodes

C++ program to reverse a linked list by changing link between nodes

How to reverse a in Singly Linked List in C++?

Reversing a linked list means to swap the position of first and last node, second and second last node and so on. In this article we will learn how to code a C++ program to reverse a linked list by changing links between nodes. If a linked list is reversed by changing its link between nodes then its head node will become tail node and its tail node will become head node. As the linked list is reversed its previous tail node i.e. the last node will no more point to NULL . Now previous head node that is the first node will point to NULL.

Steps to write a C++ program to reverse a linked list by changing links between nodes

These steps are used to reverse the linked list in CPP programming

  1. Start the program and initialize the variables.
  2. Create function to create list (listBanao).
  3. The above created function will help us to create linked list.
  4. Now create another function to reverse linked list (reverse).
  5. This function will have our main algorithm to reverse the given linked list.
  6. Now create a function to display the linked list(listDhikhao).
  7. This function will help to display the linked list whenever we want to display it.
  8. In the main method we can use these created function as per our need.

As we have followed the above steps our linked list is reversed.

Syntax

class Node  

    int data; 
    Node *next; 
}; 
C++ program to reverse a linked list by changing link between nodes

Algorithm to reverse a linked list by changing links between nodes

Below is the algorithm used to reverse linked list in C++

  • START WHILE
  • TEMP=CURRENT->NEXTPTR
  • CURRENT->NEXTPTR=PREV
  • PREV=CURRENT
  • CURRENT=TEMP
  • END WHEN CURRENT=NULL

Program to reverse singly linked list in C++

Run
#include<iostream>
using namespace std;

struct node
{
    int num;
    node* nextptr;
} * stnode; //node constructed

void createList(int n);
void reverse(node** stnode);
void displayList();

int main()
{
    int n, num, item;

    cout << "Enter the number of nodes: ";
    cin >> n;
    createList(n);

    cout << "\nLinked list data: \n";
    displayList();

    cout << "\nAfter reversing\n";
    reverse(&stnode);

    displayList();

    return 0;
}

void createList(int n)
{
    struct node* frntNode, * tmp;
    int num, i;

    stnode = new node();
    if (stnode == NULL)
    {
        cout << "Memory can not be allocated";
    }
    else
    {
        cout << "Enter the data for node 1: ";
        cin >> num;
        stnode->num = num;
        stnode->nextptr = nullptr; // Links the address field to NULL
        tmp = stnode;

        for (i = 2; i <= n; i++)
        {
            frntNode = new node();

            if (frntNode == nullptr)
            {
                cout << "Memory can not be allocated";
                break;
            }
            else
            {
                cout << "Enter the data for node " << i << ": ";
                cin >> num;
                frntNode->num = num;
                frntNode->nextptr = nullptr;
                tmp->nextptr = frntNode;
                tmp = tmp->nextptr;
            }
        }
    }
}

void reverse(node** stnode)
{
    node* temp = nullptr;
    node* prev = nullptr;
    node* current = (*stnode);

    while (current != nullptr)
    {
        temp = current->nextptr;
        current->nextptr = prev;
        prev = current;
        current = temp;
    }
    (*stnode) = prev;
}

void displayList()
{
    struct node* tmp;
    if (stnode == nullptr)
    {
        cout << "List is empty";
    }
    else
    {
        tmp = stnode;
        while (tmp != nullptr)
        {
            cout << tmp->num << "\t";
            tmp = tmp->nextptr;
        }
    }
}

Output

Enter the number of nodes: 4
Enter the data for node 1: 11
Enter the data for node 2: 22
Enter the data for node 3: 33
Enter the data for node 4: 44

Linked list data:
11 22 33 44
After reversing
44 33 22 11

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java