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 |
what is the difference between Object Based Language and Object Oriented Language?
What is role of void keyword in declaring functions?
Write a java program that prints all the values given at command-line.
What is bool mean?
What does it mean to be immutable?
What is a vararg?
what is singlton class?where it is use in real time senario.
What are streams in java 8?
Difference between a MenuItem and a CheckboxMenuItem?
How can we use primitive data types as objects?
What is the significance of java packages?
What is the numeric promotion?