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
What is a string array?
What is stable sorting method?
Which is better stack or queue?
Explain the term run-time stack?
What is the height of binary tree?
What is int data type?
Is complete binary tree?
Given an unsorted linked list, and without using a temporary buffer, write a method that will delete any duplicates from the linked list?
What are the advantage of linked list over array?
What exactly is data?
Define heap?
What is array and string?
What is best time complexity?
What is mean by abstract data type?
Why is arraylist faster than linkedlist?