In: Computer Science
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an instance of MyList, and gets the head node of this list. Please fill out the part that says “TODO”, that computes the minimum element in the list. For fun, please solve this problem recursively. This means that no part of your solution may include iterative loops. The list will not contain any integer greater than 2000000.
ListSmallest.java:
public class ListSmallest {
public static void main(String [] args) {
// Creating an instance of
MyList.
MyList L = new MyList();
// Get the head of the linked
list.
ListNode head = L.getHead();
// Create an object of this
program to avoid static context.
ListSmallest l = new
ListSmallest();
// head is the head of my linked
list L.
// TODO: please write a function to
print the minimum element in this list. Please store this in the
variable m.
int m = 0; // store the min in this
variable.
System.out.println("The smallest
is " + m);
}
}
ListNode.java:
public class ListNode
{
public int num;
public ListNode next;
public ListNode(int _num, ListNode _next)
{
num = _num;
next = _next;
}
}
MyList.java:
public class MyList
{
private ListNode head;
MyList()
{
System.out.println("Loading my list.");
for (int i = 0; i < 50000; ++i) {
this.head = new ListNode(200000 - i, this.head);
}
this.head = new ListNode(17, this.head);
for (int j = 50000; j < 100000; ++j)
{
this.head = new ListNode(200000 - j, this.head);
}
System.out.println("My list is successfully loaded. Please print
the smallest element.");
}
public ListNode getHead()
{
return this.head;
}
}
CANNOT ADJUST NODE SIZE!!
MUST SOLVE RECURSIVELY!!
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
public int findMin(ListNode head, int min) {
if (head == null) {
System.out.println("Linked List is empty");
return min + 1;
}
if (head.num < min) {
min = head.num;
} else {
min = findMin(head.next, min);
}
return min;
}
FULL CODE
class ListSmallest {
public int findMin(ListNode head, int min) {
if (head == null) {
System.out.println("Linked List is empty");
return min + 1;
}
if (head.num < min) {
min = head.num;
} else {
min = findMin(head.next, min);
}
return min;
}
public static void main(String[] args) {
// Creating an instance of MyList.
MyList L = new MyList();
// Get the head of the linked list.
ListNode head = L.getHead();
// Create an object of this program to avoid static context.
ListSmallest l = new ListSmallest();
// head is the head of my linked list L.
// function to print the minimum element in this list. storing this in the variable m.
int min = 2000000; //taking the largest possible value
int m = l.findMin(head, min); // store the min in this variable.
System.out.println("The smallest is " + m);
}
}
class ListNode {
public int num;
public ListNode next;
public ListNode(int _num, ListNode _next) {
num = _num;
next = _next;
}
}
class MyList {
private ListNode head;
MyList() {
System.out.println("Loading my list.");
for (int i = 0; i < 50000; ++i) {
this.head = new ListNode(200000 - i, this.head);
}
this.head = new ListNode(17, this.head);
for (int j = 50000; j < 100000; ++j) {
this.head = new ListNode(200000 - j, this.head);
}
System.out.println(
"My list is successfully loaded. Please print the smallest element."
);
}
public ListNode getHead() {
return this.head;
}
}