# Traverse singly linked list in reverse direction by R4R Team

Traverse Linked list in Reverse order :
In case of singly linked list, it is quit difficult to traverse the linked list in reverse order.

How we traverse a linked list:
Generally, we using first pointer we traverse all linked list element until NULL value is come.
But we want to traverse from the last so it is possible by the recursion.

Useful code:

int rtraverse(struct node *ptr)
{
if(ptr==NULL)
return 0;
printf("%d ",ptr->data);
}

program-

#include< stdio.h>
#include< conio.h>
#include< alloc.h>
struct node{
int data;
};
struct node *first=NULL;
int rtraverse(struct node *ptr)
{
if(ptr==NULL)
return 0;
printf("%d ",ptr->data);
}
int traverse()
{
struct node *ptr;
ptr=first;
while(ptr!=NULL){
printf("%d ",ptr->data);
}
}
void main()
{
struct node *cpt,*ptr;
int n,pos;
char ch;
clrscr();
first=(struct node*)malloc(sizeof(struct node));
scanf("%d",&first->data);
ptr=first;
do
{
cpt=(struct node*)malloc(sizeof(struct node));
printf("Enter another data\n");
scanf("%d",&cpt->data);
printf("Continue(y/n) ?");
ch=getch();
}while(ch=='y');
traverse();
rtraverse(first);
getch();
}

output-

Enter first element in Linked list
2
Enter another data
4
Continue(y/n)?
Enter another data
1
Continue(y/n) ?
Enter another data
9
Continue(y/n) ?
Enter another data
0
Continue(y/n) ?