## How to Reverse a Linked List by Reversing Links in C Language:

In this article, I am going to discuss How to Reverse a Linked List by Reversing Links in C Language with Examples. Please read our previous article, where we discussed How to Reverse a Linked List in C Language with Examples.

Here, we will reverse a linked list by reversing the links of the nodes.

In this linked list, node â€˜9â€™ is pointing to node â€˜7â€™, and node â€˜7â€™ is pointing to node â€™4â€™. Here reversing the links means node â€˜7â€™ will be pointing to node â€˜9â€™ instead of pointing to node â€˜4â€™. By reversing the links, we will get the linked list in reverse order as

Now before showing you how to reverse links of a linked list, I will show you one small concept that is sliding pointers. What is meant by sliding pointers? Let us see. We are taking three-pointers as

The pointer â€˜pâ€™ is pointing on the first node, â€˜qâ€™ is null and â€˜râ€™ is also null. This is the initial state means we have just initialized the pointers but we want sliding pointers or tailing pointers. In the tailing pointer, one pointer will follow the second pointer. Here are 3 pointers so we are calling them sliding pointers. So let us see how we want them to work.

Here we have moved â€˜râ€™ to â€˜qâ€™ position, â€˜qâ€™ to â€˜pâ€™ position, and â€˜pâ€™ to â€˜p->nextâ€™. So, we just move all the pointers by one node. Now â€˜qâ€™ is pointing on the first node, â€˜pâ€™ is pointing on the 2nd node and â€˜râ€™ is null. Moving all the three-pointers is a single step. All three are moving together thatâ€™s why we are calling them sliding pointers.

So, in this way we can move 3 pointers together. We want to move all the pointer until â€˜pâ€™ becomes null. We can write the code for moving sliding pointer as,

p = first;
q = NULL;
r = NULL;
while(p != NULL){
Â  Â  Â  Â  Â r = q;
Â  Â  Â  Â  Â q = p;
Â  Â  Â  Â  Â p = p->next;
}

So, in this way we can move three points together. Now let us see how we can reverse the links. Let us take a small example and show you what are the requirements for reversing the links. Suppose we have 3 nodes,

Here the first is pointing to the previous node. Now we have to reverse the link of the next node i.e. â€˜7â€™ so that it will point to the previous node i.e. â€˜9â€™. We have to modify this one. So let us take a pointer â€˜qâ€™ on node â€™7â€™ as

Now for changing qâ€™s link we should know the address of the previous node i.e. â€˜9â€™. Let us take another pointer â€˜râ€™ in the previous node i.e. â€˜9â€™ as

When this link of â€˜qâ€™ is modified then this will be pointing to â€˜râ€™ as

And now we havenâ€™t the address of the next node i.e. â€˜4â€™. Here we require another pointer â€˜pâ€™ which will point to the next node from the current node.

We need 3 pointers that are the node that is modified, the node previous to the current node, and the next node from the current node. Now we have to modify the pâ€™s link so for that we have to move â€˜qâ€™ to there and also â€˜râ€™ and â€˜pâ€™. So, we just move all the pointers by one node as,

It means that â€˜pâ€™, â€˜qâ€™, and â€˜râ€™ are sliding so that is the reason we are calling them sliding pointers. So, slide and reverse the links. Let us add a single statement to the previous code as

p = first;
q = NULL;
r = NULL;
while(p != NULL){
Â  Â  Â  Â r = q;
Â  Â  Â  Â q = p;
Â  Â  Â  Â p = p->next;
Â  Â  Â  Â q->next = r;
}

Here we add â€˜q->next = râ€™; Now let us trace these pointers on the above example.

##### Tracing of Pointers:

In the starting â€˜pâ€™ is pointing on the first node and â€˜râ€™ and â€˜qâ€™ are pointing on NULL. Here â€˜pâ€™ is not NULL so move all the pointers to next node as,

Now modify â€˜q->next = râ€™. â€˜râ€™ is pointing on null so â€˜q->nextâ€™ will be null.

This is the first step. Here â€˜pâ€™ is not NULL so move all the pointers by one node as

Here â€˜pâ€™ is pointing on â€˜4â€™, â€˜qâ€™ is pointing on â€˜7â€™ and â€˜râ€™ is pointing on â€˜9â€™. Now modify qâ€™s link as â€˜q->next = râ€™ so q will point on r as

Here â€˜q->nextâ€™ is pointing on â€˜râ€™. In this way, we will change the links to a linked list. After reversing all the links, the linked list will be

Here â€˜qâ€™ is the first node so we have to add a statement â€˜first = qâ€™ in our code. So, these are the simple steps for reversing a linked list. As we already discussed the movement of data is not preferred in a linked list so we prefer to reverse the links of a linked list. The reason behind this is that for learning purposes we are just taking integers only but it may be integers or maybe not. It can be a huge record. So, at that time we will not change the records. So thatâ€™s why it is preferred to reverse links not the data in a linked list. Now let us write complete program for reversing a linked list.

##### Program to Reverse a Linked List by Reversing Links using C Language:
```#include <stdio.h>
#include <stdlib.h>

struct Node
{
int data;
struct Node *next;
} *first = NULL;

void Create(int A[], int n)
{
int i;
struct Node *t, *last;
first = (struct Node *) malloc(sizeof(struct Node));
first->data = A[0];
first->next = NULL;
last = first;

for (i = 1; i < n; i++)
{
t = (struct Node *) malloc(sizeof(struct Node));
t->data = A[i];
t->next = NULL;
last->next = t;
last = t;
}
}

void Display(struct Node *p)
{
while (p != NULL)
{
printf ("%d ", p->data);
p = p->next;
}
}

{
struct Node *q = NULL, *r = NULL;
while (p != NULL)
{
r = q;
q = p;
p = p->next;
q->next = r;
}
first = q;
}

int main()
{
int A[] = { 3, 4, 9, 10, 13, 16, 19 };
Create (A, 5);

printf("Before Reverse: \n");
Display(first);