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

How to create an interface?

0 Answers  


What is access modifiers?

1 Answers   Cap Gemini,


how to crate clint-server socket?

1 Answers   Accenture, Infosys, Infotech, TCS,


What happens if we override only equals?

0 Answers  


Why array is used in java?

0 Answers  






What is hashtable and explain features of hashtable?

0 Answers  


What is the difference between this() and super() in java?

0 Answers  


Can a static class have a constructor java?

0 Answers  


The following program is Overloading or Overriding? public class PolymorphismEx { public int sampleMethod(int a) { return a; } public String sampleMethod(int a) { return "Is it Overloading or Overriding???"; } }

4 Answers   Ness Technologies, TCS,


What is string made of?

0 Answers  


What is unsigned char?

0 Answers  


When a byte datatype is used?

0 Answers  


Categories