logo Practice-It logo


Related Links:
Author: Marty Stepp (on 2010/12/28)

Write a method removeMin that could be added to the LinkedIntList class that removes the smallest element value from the linked list. Suppose a LinkedIntList variable named list stores the following elements:

[8, 4, 7, 2, 9, 4, 5, 3]

If you made the call of list.removeMin();, the list would then store the elements:

[8, 4, 7, 9, 4, 5, 3]

If the list has more than one occurrence of the minimum value, the first occurrence is the one that should be removed. For example, [8, 10, 13, 8, 8, 14, 9, 8, 11] becomes [10, 13, 8, 8, 14, 9, 8, 11]. If the list is empty, you should throw a NoSuchElementException.

Obey the following restrictions in your solution:

  • Do not call any other methods on the LinkedIntList object, such as add, remove, or size.
  • Do not create new ListNode objects (though you may have as many ListNode variables as you like).
  • Do not use other data structures such as arrays, lists, queues, etc.
  • Your solution should run in O(N) time, where N is the number of elements of the linked list.

Assume that you are adding this method to the LinkedIntList class (that uses the ListNode class) below.

public class LinkedIntList {                public class ListNode {
    private ListNode front;                     public int data;
    ...                                         public ListNode next;
}                                               ...
Type your solution here:

This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.