# C Program for Reversing a Linked List without changing the links between the nodes(Data Reverse Only)

## 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. ## Algorithm :

• Create a node say current and set it to head.
• Now, Create a variable to store size of linked list. We will iterate over all nodes of linked list and increment the size variable by 1 and set current to next node. Repeat this till the current node does not become  NULL.
• Create an array of size (size) to store the node value in it.And create a variable say x points to last index of the array. We will iterate over all nodes of linked list and insert the current node data in that array position and decrement the x by 1 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 x and set it to 0.
• Now, change the current node data with array[x] and increment the x by 1 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. ## Code in C based on above Algorithm

Run
```#include<stdio.h>
#include<stdlib.h>

struct Node
{
int data;
struct Node *next;
};

/* Function to reverse the linked list */
static void reverse (struct Node **head_ref)
{
int size = 0;

while (current != NULL)
{
size++;
current = current->next;
}

int a[size];

int x = size - 1;

while (current != NULL)
{
a[x--] = current->data;
current = current->next;
}
x = 0;

while (current != NULL)
{
current->data = a[x++];
current = current->next;
}
}

/* Function to push a node by user
void push(struct Node** head_ref, int new_data)
{
struct Node* new_node
= (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
}
*/

/* Function to print linked list */
{
while (temp != NULL)
{
printf ("%d  ", temp->data);
temp = temp->next;
}
}

/* Driver code*/
int main ()
{
//creating 4 pointers of type struct Node
//So these can point to address of struct type variable
struct Node *node2 = NULL;
struct Node *node3 = NULL;
struct Node *node4 = NULL;

// allocate 3 nodes in the heap
head = (struct Node *) malloc (sizeof (struct Node));
node2 = (struct Node *) malloc (sizeof (struct Node));
node3 = (struct Node *) malloc (sizeof (struct Node));
node4 = (struct Node *) malloc (sizeof (struct Node));

node2->data = 10;
node2->next = node3;

node3->data = 15;
node3->next = node4;

node4->data = 20;
node4->next = NULL;

return 0;
}
```

### Output:

`Given Linked list: 5 10 15 20Reversed Linked list: 20 15 10 5`

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

• 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