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

JAVA /** * deleteMax removes and returns the maximum number in the linked list y

ID: 3786854 • Letter: J

Question

JAVA

/**
   * 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;

Explanation / Answer

Java has given the API for LinkedList,So better we should use LinkedList(list in the given method). public int RemoveMax(LinkedList list) { int nbElements = list.size(); int higherValueIndex = 0; int higherValue = Integer.MIN; for (i = 0; i list.get(higherValueIndex) { higherValueIndex = i; higherValue=list.get(i); } } list.remove(higherValueIndex); return higherValue; }