Question

Write a method max() that takes a reference to the first node in a linked list...

Write a method max() that takes a reference to the first node in a linked list as argument and returns the value of the maximum key in the list. Assume that all keys are positive integers, and return 0 if the list is empty. In java

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public static int max(List<Integer> list){
  if(list.size() == 0){
    return 0;
  }
  else{
    int max = Integer.MIN_VALUE;
    for(int x = 0;x<list.size();x++){
      if(max < list.get(x)){
        max = list.get(x);
      }
    }
    return max;
  }
}

Please upvote the solution if it helped. Thanks!

\color{red}Note: \;Please \;comment \;below \;if \;you \;have \;any \;doubts

Add a comment
Know the answer?
Add Answer to:
Write a method max() that takes a reference to the first node in a linked list...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A linked list of integers is built using the following struct: struct node {   int data;...

    A linked list of integers is built using the following struct: struct node {   int data;   struct node *next; }; Define a function named max that returns the maximum integer in a list. The function takes one arguments, a pointer to the head of the list. The function returns an integer, which is the maximum value. If the list is empty, return zero. NOTE: You know nothing about the values in the list. They could all be negative!

  • Write a method max() which take a generic singly linked list as an argument and returns...

    Write a method max() which take a generic singly linked list as an argument and returns the maximum element reference in the list.      If the list is empty, please throw an empty collection exception.      The method prototype is defined as follows. Please write down your code in the method body enclosed in braces: public static <T extends Comparable<T>> T max(SingleLinkedList<T> list) throws EmptyCollectionException { } 2) What is the big O notation of the max method? a) O(N)...

  • Question 13 (6 points) Assume the singly linked list is defined as a head reference variable...

    Question 13 (6 points) Assume the singly linked list is defined as a head reference variable refers to the first node and a size reference variable refers to the number of nodes (similar like your L.AB4). Provide a method named count that used to count and return the occurrences of the head item (item stored at the head node) in the list. If the list is empty, return - 1. provide the appropriate method signature and implementation. Format BIU In...

  • JAVA - Write a recursive function that takes a linked list as input and returns all...

    JAVA - Write a recursive function that takes a linked list as input and returns all of the elements in the linked list. Input: 1, 2, 3, 4, 5 Output: (1+2+3+4+5)/5 = 3 What I have: public static int findMean (Node head, Node cur, int n){ int average = 0; if (head == null){ if(n == 0) return 0; } if (n > 0){ int sum = n + findMean(head, head.next, n -1); average = (sum)/n; } return average; }...

  • Write a C++ function to add a node to the beginning of a linked list. Your...

    Write a C++ function to add a node to the beginning of a linked list. Your function takes two arguments - the head of the linked list and the value num to be added. Note that the list may be empty! Your function should modify the head of the linked list to point to the new node, and set the new node to point to the rest of the list (if not empty). Example: Initial Array: 4->2->3, key = 5...

  • The following is a method for a linked list written in c++. The method is a...

    The following is a method for a linked list written in c++. The method is a simple search. It takes in a value, and returns true if the value is on the list, and false if it isn't. The function does not work 100% of the time. It will return false on an empty list, and it will return false when the value is not on the list, however, it will ONLY return true if the value passed in is...

  • (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value...

    (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value in an ArrayList of integers. The method returns null if the list is null or the list size is 0.          public static Integer max(ArrayList<Integer> list) b. Write a method that returns the sum of all numbers in an ArrayList: public static Integer sum(ArrayList<Integer> list) c. Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes...

  • 2) (10 pts) Write a function that takes in a pointer to a linked list of...

    2) (10 pts) Write a function that takes in a pointer to a linked list of nodes storing integers and a variable named value, and returns the number of nodes in the list storing that value. For example, if a list pointed to by listPtr stores 2, 6, 2, 3, 4, 2, 6, and 6 and value = 6, your function should return 3, since 6 appears in the list 3 times. Please use the struct and function prototype provided...

  • In C++ syntax please Write a program that implements and demonstrates a linked list using functions....

    In C++ syntax please Write a program that implements and demonstrates a linked list using functions. Your program should first dehne a node that stores an integer and then your program will include the following functions appendo- This function accepts the head pointer (by reference) of a linked list and an integer as it's only arguments. It then creates a node, stores the integer argument in the node, and adds it to the end of the list. findo-This function accepts...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT