solution1--reverse the list and read it..
struct node* reverseList(struct node* head)
{
struct node *ptr1,*ptr2,*first,*last;
if(head==NULL)
return NULL:
first=head;
while(head!=NULL)
{
ptr1=head;
ptr2=head->link;
if(first==head)
ptr1->link=NULL;
ptr2->link=ptr1;
head=ptr2;
if(head->link==NULL)
first=head;
}
return first;
}
solution2 : push the elements into the stack and then read the elements
struct node* reverseList(struct node* head)
{
struct node *ptr1,*ptr2,*first,*last;
if(head==NULL)
return NULL:
first=head;
while(head!=NULL)
{
ptr1=head;
ptr2=head->link;
if(first==head)
ptr1->link=NULL;
ptr2->link=ptr1;
head=ptr2;
if(head->link==NULL)
first=head;
}
return first;
}
solution2 : push the elements into the stack and then read the elements
nice code...also another good one here:
ReplyDeleteReverse a linked list
there is also a code for reverse of single linked list........
ReplyDeletevoid readfrmbck(struct node *start)
{
if(start==NULL)
return;
//printf("%d",start->data);
readfrmbck(start->next);
printf("%d",start->data);
}