Write a Java method in the LinkedList class with the followi
Write a Java method in the LinkedList class with the following header: public int[] toArray() The method should return a newly-allocated array of int values, containing all of the elements in the linked list in the same order as they are stored in the list. The array should have the same size as the list. Write a recursive Java method with the following header: public int recMinimum (int[] values, int start, int end) The method should return the minimum value stored in the array between \"start\" and \"end\" including both endpoints. For example. recMinimum([3, -1, 5, 2, -5], 0, 3) should return -1. You may assume that (0
Solution
Here is the code for you:
public static int[] addElement(int[] x, int element)
{
x = Arrays.copyOf(x, x.length + 1);
x[x.length - 1] = element;
return x;
}
public int[] toArray(Node head)
{
Node current = head;
int[] z = new int[0];
while(current != null)
{
addElement(z, current.getDatum());
current = current.getNext();
}
return z;
}
public int recMinimum(int[] values, int start, int end)
{
if(start == end)
return values[start];
return values[start] < recMinimun(values, start+1, end) ? values[start] : recMinimum(values, start+1, end);
}
![Write a Java method in the LinkedList class with the following header: public int[] toArray() The method should return a newly-allocated array of int values, c Write a Java method in the LinkedList class with the following header: public int[] toArray() The method should return a newly-allocated array of int values, c](/WebImages/26/write-a-java-method-in-the-linkedlist-class-with-the-followi-1067188-1761558421-0.webp)