Reverse a linked list without changing links between nodes (Data reverse only) in C++

Reverse a linked list without changing links between nodes (Data reverse only)

Here, in this page we will discuss Reverse a linked list without changing links between nodes in C++. We create a vector and store the elements of the linked list and them change the linked list node data by iterating the vector in reverse manner.

In this approach, we reverse the data of the linked list without altering the actual links between nodes. Instead of changing pointers, we simply swap the data values of nodes from start to end. This method keeps the structure intact while achieving the same reversed order of elements.

Reverse a linked list without changing the links

Algorithm :

  • Create a node say current and set it to head.
  • Now, Create a vector to store the node value in it. We will iterate over all nodes of linked list and push the current node data in vector and set current to next node. Repeat this till the current node does not become  NULL.
  • Now, again set current to head and take a variable say j and set it to vector.size()-1.
  • Now, change the current node data with vector[j] and decrement the j and set current to next node. Repeat this till current node does not become NULL.
  • In this way we can Reverse a linked list without changing links between nodes in C++
Reverse a linked list without changing links between nodesin c++

C++ program to reverse a linked list

In this C++ program, the linked list is reversed by modifying only the data of each node instead of changing the links between them. The program first stores all node values in a vector, then reassigns them in reverse order to achieve reversal. This method is simple and efficient when the focus is on data manipulation rather than pointer operations.

Run

#include<bits/stdc++.h>
using namespace std;

/* Link list node */
struct Node
{
  int data;
  struct Node *next;
    Node (int data)
  {
    this->data = data;
    next = NULL;
  }
};

struct LinkedList
{
  Node *head;
    LinkedList ()
  {
    head = NULL;
  }

  /* Function to reverse the linked list */
  void reverse ()
  {

    Node *current = head;

    std::vector < int >element;

    while (current != NULL)
      {
	element.push_back (current->data);
	current = current->next;
      }


    current = head;
    int i = element.size () - 1;

    while (current != NULL)
      {
	current->data = element[i--];
	current = current->next;
      }

  }

  /* Function to print linked list */

  void display ()
  {
    struct Node *temp = head;
    while (temp != NULL)
      {
	cout << temp->data << " "; temp = temp->next;
      }
  }

  void insert (int data)
  {
    Node *temp = new Node (data);
    temp->next = head;
    head = temp;
  }
};

/* Driver code*/
int main ()
{

  LinkedList ll;
  ll.insert (10);
  ll.insert (20);
  ll.insert (30);
  ll.insert (40);

  cout << "Given linked list\n";
  ll.display ();

  ll.reverse ();

  cout << "\nReversed Linked list \n";
  ll.display ();
  return 0;
}

Output :

Given linked list 
40 30 20 10
Reversed Linked list
10 20 30 40

Time & space complexity:

FunctionTime ComplexitySpace Complexity
insert()O(1)O(1)
display()O(N)O(1)
reverse()O(N)O(N)
OverallO(N)O(N)

To wrap it up:

Reversing a linked list without changing the links between nodes allows you to preserve the original structure while simply altering the data order. This method uses an auxiliary array or vector to temporarily store node values and then updates them in reverse sequence.

This technique is efficient when the node connections need to remain intact, ensuring no disruption to the overall list structure. However, it requires extra memory, making it slightly less space-efficient than reversing by changing links directly.

FAQs

Data reversal swaps the values of the nodes without altering the actual next pointers. It keeps the linked list structure intact while reversing the sequence of data.

Reversing links changes the next pointers of nodes to point backward, while data reversal only swaps the node values, leaving the links unchanged.

The time complexity is O(n) because each node’s data is visited at least once, and the space complexity can be O(1) if done iteratively using two-pointer technique.

Data reversal is useful when modifying pointers is risky or restricted, such as in read-only structures or when external references depend on existing links.

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