#include <stdlib.h>
#include <stdio.h>
struct Node
{
char data;
Node *next;
};
void printlist(Node *node)
{
while (node!=NULL)
{
printf("%c ",node->data);
node=node->next;
}
printf("\n");
}
void reverse(Node *head,Node *newhead)
{
Node *p=head;
Node *q=head;
Node *r=head;
int flag=0;
if(head==newhead)
{
flag=1;
}
q=q->next;
head->next=NULL;
while (q)
{
r=q;
q=q->next;
if(flag==0)
{
r->next=p;
p=r;
}
else
{
if(head->next==NULL)
{
head->next=r;
r->next=NULL;
}
else
{
r->next=head->next;
head->next=r;
}
}
if(flag==0 && r==newhead)
flag=1;
}
//head->next=NULL;
printlist(newhead);
}
void main()
{
Node a,b,c,d,e;
a.data='a';
a.next=&b;
b.data='b';
b.next=&c;
c.data='c';
c.next=&d;
d.data='d';
d.next=&e;
e.data='e';
e.next=NULL;
printlist(&a);
reverse(&a,&a);
}