Boost Your Technical Knowledge Forum Index
RegisterSearchFAQMemberlistUsergroupsfChatLog in
Reply to topic Page 1 of 1
Efficient way to reverse of a singly link list ?
Author Message
Reply with quote
Post Efficient way to reverse of a singly link list ? 
Here I am giving a solution to reverse of a singly list..Can you find more better way for it?
Iterative Method:

Code:
NODE* ReverseList(NODE *ptr)
{
   NODE* prevPtr= NULL;
   while(ptr)
   {
     NODE* tmp = ptr->next;
     ptr->next = prevPtr;
     prevPtr = ptr;
     ptr = tmp;
   }
   return ptr;
}


Recursive Solution:

Code:
NODE* reverseList(NODE* ptr)
{
 if(ptr == NULL) return ptr;
 else
  return( reverseList(ptr->next)->next=ptr);
}



_________________
Truth Can'nt be changed That is only one in the world "DEATH"
View user's profile Send private message Send e-mail Yahoo Messenger
Reply with quote
Post  
Hi everyone.. Can you post more efficient way to reverse a list?


_________________
Truth Can'nt be changed That is only one in the world "DEATH"
View user's profile Send private message Send e-mail Yahoo Messenger
Reply with quote
Post  
whatever you have putted..that's efficient...

View user's profile Send private message
Reply with quote
Post reverse list 
node* reverselist(node *head)
{
node *p1,*p2,*p3;
p1=head;
p2=p1->next;
p3=p2->next;
head->next=NULL;
while(p3!=NULL)
{
p2->next=p1;
p1=p2;
p2=p3;
p3=p3->next;
}
p2->next=p1;
head=p2;
return(head);
}

View user's profile Send private message
Display posts from previous:
Reply to topic Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum