create an singly linked lists and reverse the lists by
interchanging the links and not the data?
Answer Posted / guest
public class ReverseList {
public static void main(String[] args) {
ReverseList revalgo = new ReverseList ();
Node n1 = new Node();
n1.data = "A";
revalgo.insert(n1);
n1 = new Node();
n1.data = "B";
revalgo.insert(n1);
n1 = new Node();
n1.data = "C";
revalgo.insert(n1);
n1 = new Node();
n1.data = "D";
revalgo.insert(n1);
n1 = new Node();
n1.data = "E";
revalgo.insert(n1);
System.out.println("Link List");
revalgo.print();
System.out.println("Reversed Link List");
revalgo.reverse();
revalgo.print();
}
void insert(Node n)
{
n.next = root;
root = n;
}
void print()
{
Node current = root;
while(current != null)
{
System.out.print(current.data + "->");
current = current.next;
if(current == null)
System.out.println(""+ null);
}
}
void reverse()
{
Node current = null;
Node prev = null;
while(root != null)
{
current = root;
root = root.next;
current.next = prev;
prev = current;
}
root = current;
}
Node root = null;
}
class Node {
String data;
Node next;
}
Is This Answer Correct ? | 1 Yes | 2 No |
Post New Answer View All Answers
How many times is merge sort called?
Which is the simplest file structure?
What is the need for path compression?
Define the term “percolate up”?
What is the use of hashtable?
What are the advantages of modularity?
Which is faster array or linked list?
What is mean by abstract data type?
Is pointer a variable in data structure?
What is the best time complexity of bubble sort?
How do I use quick sort?
Which sorting is used in collections sort?
What are the types of collection?
What are the average and worst time complexity in a sorted binary tree is
Why is quicksort faster than merge sort?