Linked List reverese program

Answers were Sorted based on User's Feedback



Linked List reverese program..

Answer / dsr

import java.util.Collections;
import java.util.LinkedList;


public class LikedListDemo {
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.add("Raju");
list.add("Gopal");
list.add("Senthil");
list.add("nagesh");
System.out.println("list size....."+list.size());
System.out.println("list ....."+list);
Collections.reverse(list);
System.out.println("revese list ....."+list);
}
}

Is This Answer Correct ?    8 Yes 3 No

Linked List reverese program..

Answer / 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

More Core Java Interview Questions

what are the purposes of native, transiant key words?

2 Answers  


Tell me about different OOPS concepts.

0 Answers   Amdocs,


Write a program in java to establish a connection between client and server?

0 Answers  


What must a class do to implement an interface in java programming?

0 Answers  


why Java does not support multiple inheritances?

0 Answers   Aspire,






Difference between association, composition and aggregation?

0 Answers  


What will happen when using pass by reference in java?

0 Answers   HCL,


What is keyword auto for?

0 Answers  


How many types of classes are there in java?

0 Answers  


What is the top class of AWT event hierarchy?

5 Answers  


what is object slice?

2 Answers   HTC, TCS,


what is the use of declaring constructor as private?

5 Answers   Cyient, IVY Technologies, Sai Softech, Virtusa,


Categories