Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA - If incomplete what is incomplete about it? /** * deleteMax removes and re

ID: 3786062 • Letter: J

Question

JAVA - If incomplete what is incomplete about it?

/**
   * deleteMax removes and returns the maximum number in the linked list you
   * can assume there are no duplicates in the
   *
   * Examples: LinkedList : 4 --> null ==> return 4 and the resulting linked
   * list is null LinkedList : 2 --> 3 --> null ==> return 3 and resulting
   * linked list is 2 --> null LinkedList : 1 --> -3 --> null ==> return 1 and
   * resulting linked list is -3 --> null
   */

code I have:

   public int deleteMax() {
       int newMax = first.data;
       Node maxNode = first;
       Node prevNode = first;
       Node prev = null;
       if ( first == null){
           return 0;
       }
       for (Node n = first; n != null; n = n.next) {
           if (n.next.data > newMax) {
               newMax = n.data;
               maxNode = n;
               prevNode = prev;

           }
           prev = n;
       }

       prevNode.next = maxNode.next;

       return newMax; // TODO
   }

Explanation / Answer

Hi buddy, Please find the differences between this code and your old code. I've written comments for your better understanding.

public class LIST{
  
//This is the node.
static class Node{
int data;
Node next;
Node(int d){
data = d;
}
}
static Node first;
  
static int deleteMax() {
//Variable to hold the max value. This could be avoided as we are using another variable to hold the max node (maxNode)
int newMax = first.data;
//This node holds the max node and it is initialized to the first node
Node maxNode = first;
//This stores the previous node of the maximum node should be set to null
Node prevNode = null;
//Temporary variable to hold the previous node
Node prev = null;
//Good work!! Checking whether the List is empty
if ( first == null){
return 0;
}
//Since you are comparing with the next element's data, you have to make sure that the next node is also not null
for (Node n = first; n.next!=null && n != null; n = n.next) {
//This statement should come first.
prev = n;
//Checking if the next element is greater than the current
if (n.next.data > newMax) {
//If the next element is greater, update the newMax element and value and prevNode
newMax = n.next.data;
maxNode = n.next;
prevNode = prev;
}
}
//In the end, if the prevNode is null , that means the maxNode is at the beginning. So, you have to
//update the first one
if(prevNode==null){
first = maxNode.next;
return newMax;
}
//In the end , update the next element of the previous node to the next element of the max node. Nice work
prevNode.next = maxNode.next;
return newMax; // TODO
}

public static void main(String[] args) throws IOException {
//This is the data i created to test the program. It's working fine :-)
first = new Node(2);
Node a = new Node(1);
first.next = a;
Node b = new Node(4);
a.next = b;
Node c = new Node(3);
b.next = c;
Node d = new Node(5);
c.next = d;
deleteMax();
Node temp = first;
while(temp!=null){
System.out.println(temp.data);
temp = temp.next;
}
}
}