Use Java. Thank you! Write a class that provides the following three methods: 1.
ID: 3838541 • Letter: U
Question
Use Java. Thank you!
Write a class that provides the following three methods: 1. Iteratively reverse a list public static LList iterativeReverseList(LList list) that accepts a reference to a LList and returns a reference to another LList that contains the data of the original list in reverse order. Your method should be implemented using iteration. 2. Recursively Reverse a list public static LList recursiveReverseList(LList list) that accepts a reference to a LList and returns a reference to another LList that contains the data of the original list in reverse order. Your method should be implemented using recursion. 3. Sort Using ArrayList public static ArrayList insertSort(ArrayList list) that accepts a reference to an ArrayLIst and returns another ArrayList that contains the data of the original list in ascending order. You can implement your method as insertion sort or selection sort. You cannot use the Java sort methods. Write a test clExplanation / Answer
Please find the below working code
***********************
package com.icodejava.blog.datastructure;
public class LinkedListReverse {
public static void main(String args[]) {
//Preparing some linked list structure
LinkedList linkedList = new LinkedList(5);
linkedList.next = new LinkedList(4);
linkedList.next.next = new LinkedList(3);
linkedList.next.next.next = new LinkedList(2);
linkedList.next.next.next.next = new LinkedList(1);
System.out.println("Original Linked List: " + linkedList.toString());
//recursively reverse and print
linkedList = recursiveReverse(linkedList);
System.out.println("Recursively Reversed List: "
+ linkedList.toString());
//iteratively reverse and print
linkedList = iterativeReverse(linkedList);
System.out.println("Iteratively Recursed to Original: "
+ linkedList.toString());
linkedList = ArrayList(LinkedList);
}
}
/**
* This method uses recursive method to reverse a singly linked list.
*/
public static LinkedList recursiveReverse(LinkedList linkedList) {
// check for empty or size 1 linked list. This is a base condition to
// terminate recursion.
if (linkedList == null || linkedList.next == null) {
return linkedList;
}
LinkedList remainingReverse = recursiveReverse(linkedList.next);
// update the tail as beginning
LinkedList current = remainingReverse;
while (current.next != null) {
current = current.next;
}
// assign the head as a tail
current.next = linkedList;
linkedList.next = null;
return remainingReverse;
}
/**
* This method uses iterative approach to reverse a singly linked list.
*/
public static LinkedList iterativeReverse(LinkedList linkedList) {
if (linkedList == null || linkedList.next == null) {
return linkedList;
}
LinkedList prevNode, currNode, nextNode;
prevNode = null;
nextNode = null;
currNode = linkedList;
while (currNode != null) {
nextNode = currNode.next;
currNode.next = prevNode;
prevNode = currNode;
currNode = nextNode;
}
return prevNode;
}
}
public static ArrayList sortArray(ArrayList arrayList){
int iArr[] = arrayList;
// let us print all the elements available in list
for (int number : iArr) {
System.out.println("Number = " + number);
}
// sorting array
Arrays.sort(iArr);
System.out.println("The sorted int array is:");
for (int number : iArr) {
System.out.println("Number = " + number);
return iArr[];
}
/**
* Custom Linked List representation class
*/
class LinkedList {
public LinkedList next;
public int value;
public LinkedList(int value) {
this.value = value;
this.next = null;
}
@Override
public String toString() {
String data = "";
LinkedList current = this;
do {
data += current.value + ",";
current = current.next;
} while (current != null);
return data;
}
}
**********************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.