How can we reverse a singly linked list??Reversal here means we have to reverse the links not the values.
So here is the code:
void reverse(struct node **ps)
{
struct node *temp,*temp1,*temp2;
temp=*ps;
temp2=NULL;
while(temp!=NULL)
{
temp1=temp2;
temp2=temp;
temp=temp->next;
temp2->next=temp1;
}
*ps=temp2;
}
Here as we see the value of start will be changed.So we have used pointer to pointer.This function can be called from main as: reverse(&start);
So here is the code:
void reverse(struct node **ps)
{
struct node *temp,*temp1,*temp2;
temp=*ps;
temp2=NULL;
while(temp!=NULL)
{
temp1=temp2;
temp2=temp;
temp=temp->next;
temp2->next=temp1;
}
*ps=temp2;
}
Here as we see the value of start will be changed.So we have used pointer to pointer.This function can be called from main as: reverse(&start);
No comments:
Post a Comment