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

Reverse a linked list without changing links between nodes. What do you mean by that?
We don’t have to change the links in between nodes we just have to change the matter or number over there with the other number in a linked list for the purpose of reversal. We have to change the data, in the same way we done it in a diagram we can say that  To make thing’s work we have to take two pointer’s that is left pointer and right pointer. Left pointer points to the 0th node and the right pointer points to the last node left node. In the continuation on this page we will learn about algorithm and a java program.

## Algorithm to reverse a linked list Data.

Step 1– Initialize a class.

Step 2– make head as null.

Step 3-assign tail=null also.

Step 4– A function to find the size of linked list i.e (this.size).

Step 5– Further check whether linked list is empty, (this.size() ==0).

Step 6– Traverse the node and print the linked list (node temp = head)

Step 7-print (temp.data).

Step  8– temp = temp.next

Step 9 – print (“End”).

Step 10– First add a node in at the beginning of  linked list.

Step 11– create a temp node which points towards head

Step 12– Then find if the linked list is empty.

Step 13– if it’s not then set the head such that it now points to temp node.

Step 14– Furthermore add a node at any index

Step 15– Throw new exception (“Index out of bound”)

Step 16– return temp

Step 17– take the left and right nodes to be swapped

int left=0

int right=this.size.

Step 18– swap left and right node data .

Step 19– Data will be reversed. ## Java program to reverse a linked list without changing links between nodes

Run
```class LinkedList
{

// Node Class
class Node
{
int data;
Node next;

Node (int x)		// parameterized constructor
{
data = x;
next = null;
}
}

public void display ()
{
while (temp != null)
{
System.out.print (temp.data + " ");
// Set temp to point to the next node
temp = temp.next;
}
System.out.println ("END");
}

public Node insertBeginning (int data)
{
Node newNode = new Node (data);

}
public void reverse ()
{
Node previous = null, current = null;

while (pointer != null)
{
current = pointer;
pointer = pointer.next;

current.next = previous;
previous = current;
}

}

}

public class Main
{

public static void main (String[]args)
{
try
{
ll.insertBeginning (2);
ll.insertBeginning (4);
ll.insertBeginning (6);
ll.insertBeginning (8);

ll.display ();

ll.reverse ();
ll.display ();
}
catch (Exception e)
{
System.out.println ("Exception occurred.");
}
}
}

```

#### Output

```LinkedList before reversal :
8 6 4 2 END
2 4 6 8 END
```

### 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

• Introduction to Linked List in Data Structure
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
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

• 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

### One comment on “Reverse a linked list without changing links between nodes (Data reverse only)”

• Aakriti

//in c//
#include
#include

struct node{
int val;
};

struct node* insert(struct node* head,int val)
{
struct node* node=(struct node*)malloc(sizeof(struct node));

}

{
while( nodes != NULL)
{
printf(“%d”,nodes->val);

}
}
{
struct node *nodes;
struct node* exp;
int n=6;
int i; //reverse the data of the linked list//
for(i=1;i<=3;i++) //three times running
{
int j;
nodes=temp;

}
exp->val=temp->val;
temp->val=nodes->val;
nodes->val=exp->val;
–n;
}
}
int main()
{
struct node * head =(struct node*)malloc(sizeof(struct node)); 0