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 OOP Language?

842


What is meant by object?

791


Why are constructors used?

750


What is slash r?

761


Why does java not support pointers?

779


Why is string builder not thread safe?

843


What is the largest long allowed by java?

787


Why put method is idempotent?

659


What are multiple inheritances? Is it supported by java?

676


What is the difference between a choice and a list?

835


What is final keyword in java?

785


What are the problems faced by java programmers who don't use layout managers?

746


How will you reverse a link list without using recursion?

797


How do you add an element to an arraylist in java?

708


Why do we need data structure in java?

817