Question

In: Computer Science

You are given a reference to the head node of a linked list that stores integers....

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!!

Solutions

Expert Solution

Adding the code for the class ListSmallest. Created a recursive method findMin which returns the min value of the linked list by the recursive method. Taken the min value: 2000000, as the data cannot be greater than this value.

Added a variable min to hold this largest possible value. If the linked list is null, we return the min value as 2000001 indicating that data is not valid.

Else, we proceed with finding the min value among the given nodes data.

The only change is in this calls ListSmallest. All the other classes remain the same.

public 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);
}
}


Related Solutions

You are given a reference to the head node of a linked list that stores integers....
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...
You are given a reference to the head node of a linked list that stores integers....
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...
You are given a reference to the head node of a linked list that stores integers....
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...
You're given the pointer to the head node of a ordered linked list, an integer to...
You're given the pointer to the head node of a ordered linked list, an integer to add to the list. Write a function that inserts a number in the the list preserving its order. If the head pointer contains a null pointer that indicates an empty list. Function insertNode has the following parameters: head: a SinglyLinkedListNode pointer to the head of the list data: an integer value to insert as data in your new node Function prototype: SinglyLinkedListNode* insertNode(SinglyLinkedListNode* head,...
Python 3 Function which takes the head Node of a linked list and sorts the list...
Python 3 Function which takes the head Node of a linked list and sorts the list into non-descending order. PARAM: head_node The head of the linked list RETURNS: The node at the head of the sorted linked list. ''' def sort(head_node): #Code goes here ''' Test code goes here '' ' if __name__ == '__main__':
C++ language or Python. Linked Lists You are given a linked list that contains N integers....
C++ language or Python. Linked Lists You are given a linked list that contains N integers. You are to perform the following reverse operation on the list: Select all the subparts of the list that contain only even integers. For example, if the list is {1,2,8,9,12,16}, then the selected subparts will be {2,8}, {12,16}. Reverse the selected subpart such as {8,2} and {16,12}. The list should now be {1,8,2,9,16,12}. Your node definition should consist of 2 elements: the integer value...
Given a linked list of integers, remove any nodes from the linked list that have values...
Given a linked list of integers, remove any nodes from the linked list that have values that have previously occurred in the linked list. Your function should return a reference to the head of the updated linked list. (In Python)
Working on a c++ data structures assignment.   Linked List add node. I have the head case...
Working on a c++ data structures assignment.   Linked List add node. I have the head case and the tail case working but the middle/general case I can not get to work for the life of me. I have included the header file and the data struct file below   #ifndef LINKEDLIST_H #define LINKEDLIST_H #include "data.h" #include <iostream>   //take this out using std::cout; class LinkedList{     public:         LinkedList();         ~LinkedList();         bool addNode(int, string);         bool deleteNode(int);         bool getNode(int, Data*);         void printList(bool = false);         int getCount();         void...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT