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 have the following code: //Set.java import java.util.ArrayList; public class Set<T> { //data fields private ArrayList<T>...
I have the following code: //Set.java import java.util.ArrayList; public class Set<T> { //data fields private ArrayList<T> myList; // constructors Set(){ myList = new ArrayList<T>(); } // other methods public void add(T item){ if(!membership(item)) myList.add(item); } public void remove(T item){ if(membership(item)) myList.remove(item); } public Boolean membership(T item){ for (T t : myList) if (t.equals(item)) return true; return false; } public String toString(){ StringBuilder str = new StringBuilder("[ "); for(int i = 0; i < myList.size() - 1; i++) str.append(myList.get(i).toString()).append(", "); if(myList.size()...
Remove the minimum element from the linked list in Java public class LinkedList {      ...
Remove the minimum element from the linked list in Java public class LinkedList {       // The LinkedList Node class    private class Node{               int data;        Node next;               Node(int gdata)        {            this.data = gdata;            this.next = null;        }           }       // The LinkedList fields    Node head;       // Constructor    LinkedList(int gdata)   ...
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); }   ...
Write java code to reverse a linked list. Fill in reverseLists() ReverseLinkedList.java package mid; public class...
Write java code to reverse a linked list. Fill in reverseLists() ReverseLinkedList.java package mid; public class ReverseLinkedList {     private static class ListNode {         int val;         ListNode next;         ListNode() {         }         ListNode(int val) {             this.val = val;         }         ListNode(int val, ListNode next) {             this.val = val;             this.next = next;         }     }     public static void printList(ListNode l1) {         ListNode cur = l1;         while(cur != null) {             System.out.println(cur.val);             cur = cur.next;         }     }     public static ListNode reverseLists(ListNode h1) {     }     public static void...
haskell : write a function that reverse the first three element of a list, but not...
haskell : write a function that reverse the first three element of a list, but not the rest. example [1,2,3,4,5,6] == [3,2,1,4,5,6]
get the minimum element from linked list c++
get the minimum element from linked list c++
In C++, Write a function to reverse the nodes in a linked list. You should not...
In C++, Write a function to reverse the nodes in a linked list. You should not create new nodes when you reverse the the linked list. The function prototype:          void reverse(Node*& head); Use the following Node definition: struct Node {    int data;    Node *next; }
/* *fix the below C Program to Display the Nodes of a Linked List in Reverse...
/* *fix the below C Program to Display the Nodes of a Linked List in Reverse */ #include <stdio.h> #include <stdlib.h> struct node { int visited; int a; struct node *next; }; int main() { struct node *head = NULL; generate(head); printf("\nPrinting the list in linear order\n"); linear(head); printf("\nPrinting the list in reverse order\n"); display(head); delete(head); return 0; } void display(struct node *head) { struct node *temp = head, *prev = head; while (temp->visited == 0) { while (temp->next !=...
I need to create a linked list that contains a fixed arraylist. Each new entry is...
I need to create a linked list that contains a fixed arraylist. Each new entry is added to array list. If the arraylist is full, create a new arraylist and add to the linklist. In java please.
Given two sorted linked lists, merge them into a third sorted linked list. If an element...
Given two sorted linked lists, merge them into a third sorted linked list. If an element is present in both the lists, it should occur only once in the third list. Code needed in java.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT