REVERSE A LINKED LIST

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

struct node
{
int data ;
struct node* link ;
};

struct node* head = NULL;
void insert(int data)
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
temp->link = head ;
head = temp;
}
void print()
{
struct node* temp = head ;
while(temp!=NULL)
{
printf("%d",temp->data);
temp= temp->link;
}
printf("\n");
}
void reverse()
{
struct node *current , *prev , *next;
current = head ;
prev = NULL;
while(current!=NULL)
{
next = current->link;
current->link = prev;
prev = current;
current = next;
}
head = prev;
}
void main()
{
insert(3);
insert(4);
insert(5);
insert(6);
print();
reverse();
print();
}

Comments