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

import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list =...
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list = arrayList; return list; } public static void printList(ArrayList<String> arrayList) { System.out.println("Printing in 4 ways\n"); // 1 System.out.println(arrayList); //2 for(String s:arrayList) System.out.print(s+" "); System.out.println(); //3 System.out.println(Arrays.deepToString(list.toArray())); //4 for(int i=0;i<arrayList.size();i++) System.out.print(arrayList.get(i)+" "); System.out.println(); } public static void filterList(ArrayList<String> arrayList) { System.out.println("Filtered in 2 ways\n"); ArrayList<String> copyArrayList = arrayList; //1 for(int i=0;i<arrayList.size();i++) { if(arrayList.get(i).contains("chuck")) { arrayList.remove(i); i--; } } System.out.println(arrayList); //2 copyArrayList.removeIf(str -> str.contains("chunk")); System.out.println(copyArrayList); }   ...
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.
C++: Write a reverse function that receives a reference to a integer linked list and reverses...
C++: Write a reverse function that receives a reference to a integer linked list and reverses the order of all the elements in it. For example, if the input linked list is 1 -> 4-> 2-> 3-> 6-> 5}, after processing by this function, the linked list should become 5-> 6-> 3-> 2-> 4-> 1. You need to write a main file to insert elements into the linked list and call the reverseLinkedList() function which takes the reference of first...
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...
/* 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;...
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...
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
How to reverse linked list below,thank you! #include <stdlib.h> #include <stdio.h> struct list { int data;...
How to reverse linked list below,thank you! #include <stdlib.h> #include <stdio.h> struct list { int data; struct list *next; }; typedef struct list node; typedef node *link; int main() { link ptr,head; int num,i; head = ( link ) malloc(sizeof(node)); ptr = head; printf("enter 10 data \n"); for ( i = 0; i <= 9; i++ ) { scanf("%d",&num); ptr->data = num; ptr->next = ( link ) malloc(sizeof(node)); if ( i == 9 ) ptr->next = NULL; else ptr =...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT