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


Please Help Members By Posting Answers For Below Questions

How many times is merge sort called?

602


Which is the simplest file structure?

633


What is the need for path compression?

619


Define the term “percolate up”?

595


What is the use of hashtable?

573






What are the advantages of modularity?

588


Which is faster array or linked list?

537


What is mean by abstract data type?

608


Is pointer a variable in data structure?

604


What is the best time complexity of bubble sort?

602


How do I use quick sort?

578


Which sorting is used in collections sort?

567


What are the types of collection?

556


What are the average and worst time complexity in a sorted binary tree is

629


Why is quicksort faster than merge sort?

624