Question

In: Computer Science

public java.util.ArrayList<T> getReverseArrayList() Returns an ArrayList with the element of the linked list in reverse order....

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

8.5.2

Solutions

Expert Solution

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:-)


Related Solutions

I need a MIPS Assembly program that "Display the elements of the linked list in reverse...
I need a MIPS Assembly program that "Display the elements of the linked list in reverse order." It needs subprogram and those subprogram does not have t registers.
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse();...
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse(); //reverses the order of elements in the linked list void insert(int value); private: struct Node{ int data; Node* next; Node* prev; }; Node* head; Node* tail; //Add your helper function here that recursively reverses the order of elements in the linked list }; Write the declaration of a helper function in the class provided above that recursively reverses the order of elements in the...
public List<Item> findItems(Lookup query) { ArrayList<Item> matches = new ArrayList<Item>(); for (int i = 0 ;...
public List<Item> findItems(Lookup query) { ArrayList<Item> matches = new ArrayList<Item>(); for (int i = 0 ; i < numItems ; i++ ) { Item item = items[i]; if (query.matches(item)) { matches.add(item); } } return matches; } "To improve readability, each line of code should be indented under its parent (i.e. methods should be indented under the class, code in methods should be indented under the method, code in if statements should be indented under the if, etc.). This if rcurly...
Implement a non-recursive reverse print of linked list using stack and the main function to test:...
Implement a non-recursive reverse print of linked list using stack and the main function to test: You will need to finish the printReversed_nonrecursive method in ch04.LinkedStack2 class, and the ch04.UseStack2 is the main function to test. public class LinkedStack2<T> extends LinkedStack<T> { private void revPrint(LLNode<T> listRef) { if (listRef != null) { revPrint(listRef.getLink()); System.out.println(" " + listRef.getInfo()); } } public void printReversed() { revPrint(top); } /* use stack to implement non-recursive reverse print */ public void printReversed_nonrecursive() { } public...
/* print_reverse function that prints the nodes in reverse order if the list is empty print...
/* print_reverse function that prints the nodes in reverse order if the list is empty print nothing */ include <bits/stdc++.h> using namespace std; class Node{     public:     int data;     Node *next;     Node *prev;     Node(int d, Node *p=NULL, Node *n=NULL){         data = d;         prev = p;         next = n;     } }; class DLL{     public:     Node *head;     DLL(){head=NULL;}     void push(int d){         Node *pNode = new Node(d,NULL,head);         if (head != NULL)             head->prev = pNode;         head = pNode;     }     void print_forward(){         Node *pCurr = head;...
Please use Python to create a method for a linked list that returns the index of...
Please use Python to create a method for a linked list that returns the index of a lookup value within the linked lust
Task 1: [10 Marks] Write a function “reverse” in your queue class (linked list implementation) that...
Task 1: [10 Marks] Write a function “reverse” in your queue class (linked list implementation) that reverses the whole queue. In your driver file (main.cpp), create an integer queue, push some values in it, call the reverse function to reverse the queue and then print the queue.
write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
Using python. Produce a method for a linked list that is called FIND , which returns...
Using python. Produce a method for a linked list that is called FIND , which returns the index of a lookup value within the linked list
write the method “getMaxValue” that finds and returns the maximum value in an integer linked list....
write the method “getMaxValue” that finds and returns the maximum value in an integer linked list. If the list is empty, then it should return 0. use the provided code below public class Question03 { public class ListNode//public for testing purposes { public int data;//public for testing purposes public ListNode link;//public for testing purposes public ListNode(int aData, ListNode aLink) { data = aData; link = aLink; } } public ListNode head;//public for testing purposes public int getMaxValue() { //----------------------------------------------------------------------------------- //Write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT