Reverse a singly linked list without using any extra storage other than storage of nodes.
node *save,*ptr1,*ptr2;
ptr1=list;
ptr2=list;
//Initial case to make the first node pointer(to make last) to null
ptr2=ptr1->next;
ptr1->next=NULL;
ptr1=ptr2;
while(ptr1!=NULL)
{
save=ptr1;
ptr1=ptr2;
ptr2=ptr1.next;
ptr1->next=save;
}
node *save,*ptr1,*ptr2;
ptr1=list;
ptr2=list;
//Initial case to make the first node pointer(to make last) to null
ptr2=ptr1->next;
ptr1->next=NULL;
ptr1=ptr2;
while(ptr1!=NULL)
{
save=ptr1;
ptr1=ptr2;
ptr2=ptr1.next;
ptr1->next=save;
}
No comments:
Post a Comment