# C Program for Reverse a linked list

In this problem, we are given a linked list. Our task is to create a program for reverse a linked list.

The program will reverse the given linked list and return the reversed linked list.

## Example

9 -> 32 -> 65 -> 10 -> 85 -> NULL

Reverse linked list is a linked list created to form a linked list by reversing the links of the list. The head node of the linked list will be the last node of the linked list and the last one will be the head node.

## Example

85 -> 10 -> 65 -> 32 -> 9 -> NULL

To reverse the given linked list we will use three extra pointers that will be in the process. The pointers will be previous, after, current.

We will initialize previous and after to NULL initially and current to head of the linked list.

After this, we will iterate until we reach the NULL of the initial (non-reversed linked list). And do the following −

after = current ->
next current ->
next = previous
previous = current
current = after

Now let’s create a program for reversing the linked list. There can be two ways to create the program, one is iterative and the other one is recursive.

Program for Reverse a linked list (tail-recursive approach)

## Example

Live Demo

#include <stdio.h>
struct Node {
int data;
struct Node* next;
};
Node* insertNode(int key) {
Node* temp = new Node;
temp->data = key;
temp->next = NULL;
return temp;
}
void tailRecRevese(Node* current, Node* previous, Node** head){
if (!current->next) {
current->next = previous;
return;
}
Node* next = current->next;
current->next = previous;
}
return;
}
}
printf("");
}
int main(){
return 0;
}

## Output

Linked list : 9 32 65 10 85
Reversed linked list : 85 10 65 32 9

Program for Reverse a linked list (iterative approach)

## Example

Live Demo

#include <stdio.h>
struct Node {
int data;
struct Node* next;
Node(int data){
this->data = data;
next = NULL;
}
};
}
void interReverseLL(){
Node *prev = NULL, *after = NULL;
while (current != NULL) {
after = current->next;
current->next = prev;
prev = current;
current = after;
}
}
void print() {
while (temp != NULL) {
printf("%d ", temp-> data);
temp = temp->next;
}
printf("");
}
void push(int data){
Node* temp = new Node(data);
}
};
int main() {
}
Linked List : 9 32 65 10 85
Reverse Linked List : 85 10 65 32 9