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 |
Can a constructor call another constructor?
What will happen if there is a default method conflict as mentioned above and we have specified the same signature method in the base class instead of overriding in the existing class ?
What is regex java?
What are the basics of core java?
Can we have any code between try and catch blocks?
Is string is a keyword in java?
Can we execute a program without main?
What is the primitive type byte?
What is integer valueof?
How can u create the Object of class Without using "New" opertor?
Explain the init method?
how a programmer confirms that the data submitted has been succesfully inserted into the database(either oracle or my sql).. How a programmer confirm if there is any problem with the program he wrote for insertion