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
If you are using c language to implement the heterogeneous linked list, explain what pointer type will you use?
What is linear-logarithm chasm?
What is the difference between a hashmap and hashtable?
What is the easiest sorting method to use in data structures?
What is a treemap chart?
How do you represent a linked list?
What is the difference between hashmap and linkedhashmap?
Which is better merge or quick sort?
write a code for Implementation of stack and queues.
Can a hashset contain duplicates?
What is the difference between Array and LinkedList?
What is variable size arrays?and why we use it?
What is the difference between hashset and hashmap?
What are linked lists good for?
What is data type and its types?