Linked List reverese program
Answers were Sorted based on User's Feedback
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 |
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 |
How do we make a class serialize?
Program to output as below formate: 1 2 3 4 5 6 7 8 9 10
How do you sort in java?
If I only change the return type, does the method become overloaded?
What language is an assembler written in?
What is string length in java?
How to split arraylist elements in java?
Can main() method in java can return any data?
Is java a digit method?
diffrence b\w println() and printf()
What is a super method?
What is meant by data hiding/encapsulation?