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

If you are using c language to implement the heterogeneous linked list, explain what pointer type will you use?

618


What is linear-logarithm chasm?

546


What is the difference between a hashmap and hashtable?

440


What is the easiest sorting method to use in data structures?

559


What is a treemap chart?

451






How do you represent a linked list?

507


What is the difference between hashmap and linkedhashmap?

465


Which is better merge or quick sort?

560


write a code for Implementation of stack and queues.

528


Can a hashset contain duplicates?

490


What is the difference between Array and LinkedList?

563


What is variable size arrays?and why we use it?

623


What is the difference between hashset and hashmap?

535


What are linked lists good for?

472


What is data type and its types?

535