Linked List reverese program

Answer Posted / kaustubh

I'll give the algo here.You may write it in a programming
language of your choice.

Iterative Algo:

node *Reverse(node *head)
{
node *p,*q,*r;
p=head;q=r=NULL;
while(p!=NULL)
{
q=p;
p=p->next;
q->next=r;
r=q;
}
head=q;
return head;
}

Recursive algo:

From main call: Reverse(node *head,NULL)

reverse(node *p,node *q)
{
if(p->next!=NULL)
reverse(p->next,p)
else
{
p->next=q;
return
}
}

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between a choice and a list?

859


When should the method invokelater() be used?

780


Define max and min heap, also the search time of heap.

847


What is the private method modifier?

841


Why do we use return statement?

794


Is hashset sorted in java?

896


Difference between class#getinstance() and new operator ?

913


What do you understand by final value?

818


What is threaded programming and when is it used? : Java thread

797


Can a constructor be protected?

825


What are keywords in programming?

800


What is a double vs float?

775


what is the significance of listiterator in java?

845


Can we use a default constructor of a class even if an explicit constructor is defined?

874


What is a pattern what is an anti pattern?

726