C++ program to reverse a linked list by changing links 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
Reversing a linked list by changing links in C++ means adjusting each node’s pointer so that it points to the previous node instead of the next. This allows the list to be traversed in reverse order without creating a new list.
The process involves iterating through the list while keeping track of the current, previous, and next nodes to ensure no node is lost. After updating all links, the head is set to the last node, completing the reversal.
These steps are used to reverse the linked list in CPP programming
- Start the program and initialize the variables.
- Create function to create list (listBanao).
- The above created function will help us to create linked list.
- Now create another function to reverse linked list (reverse).
- This function will have our main algorithm to reverse the given linked list.
- Now create a function to display the linked list(listDhikhao).
- This function will help to display the linked list whenever we want to display it.
- 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;
};
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
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Program to reverse singly linked list in C++
Reversing a singly linked list in C++ involves changing the next pointers of each node so that they point to the previous node, effectively flipping the list’s direction. This operation is commonly implemented using iterative or recursive methods to traverse and reverse the links efficiently.
#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
Explanation:
- The program creates a singly linked list of user-defined size by dynamically allocating nodes and linking them.
- It reverses the linked list in-place by changing the nextptr of each node using three pointers: prev, current, and temp.
- The list is displayed by traversing from the head and printing each node’s data.
- Dynamic memory allocation allows flexible list size, but memory is not explicitly freed in this code.
- The main function controls the flow: creation, display, reversal, and final display of the linked list.
Time and Space Complexity :
| Function / Operation | Time Complexity | Space Complexity |
|---|---|---|
| createList(n) | O(n) – Traverses and creates n nodes | O(n) – Allocates memory for n nodes dynamically |
| reverse(stnode) | O(n) – Iterates through all n nodes once | O(1) – Uses constant extra pointers (prev, current, temp) |
| displayList() | O(n) – Visits each of the n nodes to print | O(1) – Only uses temporary pointer for traversal |
| Overall Program | O(n) – Dominated by createList and reverse operations | O(n) – Dominated by node allocations |
To wrap it up:
Reversing a linked list by changing the links between nodes is a key concept in C++ that helps in understanding pointer manipulation. By carefully adjusting the direction of each node’s pointer, we can transform the list efficiently without creating a new one.
The process uses three pointers to traverse the list, updating connections step by step until the entire list is reversed. This method is simple, time-efficient, and requires minimal extra memory, making it a practical solution for many linked list problems.
FAQs
Reversing a linked list by changing links involves modifying the next pointers of each node so that they point to the previous node instead of the next. This way, the list is reversed without altering the node data.
The time complexity is O(n) since each node is visited once, and the space complexity is O(1) because no extra memory is needed aside from a few pointer variables.
Yes, it can be done iteratively by using three pointers (prev, current, next) or recursively by reversing the rest of the list and updating the links as the recursion unwinds.
No, only the links between nodes are changed. The actual data inside each node remains unchanged while the order of nodes is reversed.
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
Singly Linked List
- Introduction to Linked List in Data Structure
Click Here - Linked List in –
- Singly Linked List in –
- Insertion in singly Linked List –
- Insertion at beginning in singly Linked List –
- Insertion at nth position in singly Linked List –
- Insertion at end in singly Linked List –
- Deletion in singly Linked List –
- Deletion from beginning in singly linked list :
- Deletion from nth position in singly linked list :
- Deletion from end in singly linked list :
- 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 –
- Print reverse of a linked list without actually reversing –
- Print reverse of a linked list without actually reversing –
- Insertion in the middle Singly Linked List –
- Insertion in a Sorted Linked List –
- Delete alternate nodes of a Linked List –
- Find middle of the linked list –
- Reverse a linked list in groups of given size –
- Find kth node from end of the linked list –
- Append the last n nodes of a linked list to the beginning of the list –
- Check whether linked list is palindrome or not –
- Fold a Linked List –
- Insert at given Position –
- Deletion at given Position –
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
- Deletion 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

Login/Signup to comment