In: Computer Science
public java.util.ArrayList<T> getReverseArrayList()
Returns an ArrayList with the element of the linked list in reverse order. This method must be implemented using recursion.
public BasicLinkedList<T> getReverseList()
Returns a new list with the elements of the current list in reverse order. You can assume sharing of data of each node is fine. This method must be implemented using recursion.
JAVA
Implementation in JAVA:
import java.util.ArrayList;
import java.util.Scanner;
import LInkedlist.Node;
public class ReverseList<T> {
// main method or driver method
public static void main(String[] args) {
System.out.println("--------------REVERSE LINKED
LIST----------\n");
System.out.println("Enter Data for
Nodes (Enter (-1) for stop.)");
Node<Integer> head= takeinput();
System.out.print("\nLinked List is : ");
print(head);
System.out.print("\n\nReverse List : ");
head=reverse(head);
print(head);
System.out.print("\n\nAgain Reverse : ");
head=reverse(head);
print(head);
System.out.println("\n\n\n ---------REVERSE ARRAYLIST
RECURSIVELY----------: ");
ArrayList<String> list = new ArrayList();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
list.add("F");
list.add("G");
System.out.println("\nArrayList : "+list);
ReverseList<String> rl = new
ReverseList<String>();
list = rl.reverseArrayList(list);
System.out.println("\nReversed ArrayList : "+list);
}
// print the Linked List
public static void print(Node<Integer> head)
{
Node<Integer>
temp=head;
while(head!=null) {
if(head.next!=null) {
System.out.print(head.data+"->");
}
else {
System.out.print(head.data+ "");
}
head=head.next;
}
}
// will reverse Linked recursively
public static Node <Integer>
reverse(Node<Integer> head){
// base condition
if(head==null||head.next==null)
{
return
head;
}
// recursive call
Node<Integer>
finalhead=reverse(head.next);
// store temporary
Node<Integer>
temp=finalhead;
// loop untill temp is not
null
while(temp.next!=null) {
temp=temp.next;
}
temp.next=head;
head.next=null;
return finalhead;
}
// it also reverse LinkedList more efficiently than
previous one
// implementing using DoubleNode
public static Doublenode
reversebetter(Node<Integer>head) {
// base case
if(head==null|| head.next==null)
{
Doublenode ans=
new Doublenode();
ans.head=head;
ans.tail=head;
return
ans;
}
// recursive call and linked
Doublenode smallans=
reversebetter(head.next);
smallans.tail.next=head;
head.next=null;
// new node ans linked to
head;
Doublenode ans= new
Doublenode();
ans.head=smallans.head;
ans.tail=head;
// return newnode
return ans;
}
// ask input form user and create a linked list
// (-1) for stop
// and will return head refrence of linked list
public static Node<Integer> takeinput() {
// initialize head
Node<Integer> head=
null;
// create object of Scanner
class
Scanner s= new
Scanner(System.in);
int data=s.nextInt();
// will take input untill user
entered -1 for stopping enter inputs
while(data!=-1) {
// new node
Node<Integer> newnode=new
Node<Integer>(data);
// if linked list is empty
if(head==null) {
head=newnode;
}
else {
Node<Integer> temp=head;
while(temp.next!=null) {
temp=temp.next;
}
temp.next=newnode;
}
// again asking to enter
data=s.nextInt();
}
// return head refrence of linked
list
return head;
}
// will return reverse ArrayList recursively
public ArrayList<T> reverseArrayList(ArrayList<T> list)
{
if(list.size() > 1) {
// temporary store last
element
T temp = list.remove(0);
// recursive call
reverseArrayList(list);
// add temp in list
list.add(temp);
}
return list;
}
}
//Node class
class Node<T> {
T data;
Node next;
Node(T data){
{
this.data=data;
next=null;
}
}
}
// Doublenode class for reverse linked efficiently
(Recursively)
class Doublenode {
Node<Integer> head;
Node<Integer> tail;
}
SAMPLE OUTPUT;
// PLEASE THUMBS-UP AND RATE POSITIVELY
If you have any doubt regarding this question please ask me in
commnets
// THANK YOU:-)