This is a beta version of Practice-It. Give us feedback

logo Practice-It logo

removeRange

Related Links:
Author: Allison Obourn (on 2018/05/01)

Write a method removeRange that could be added to the LinkedIntList class from lecture and section. The method takes two integer parameters, min and max, and removes all elements from the list whose values are between min and max, inclusive. It should return the number of elements removed.

Suppose a LinkedIntList variable named list stores the following values:

[4, 2, 1, 10, 15, 8, 7, 4, 20, 36, -3, 40, 5]

The call of list.removeRange(4, 20) would return 8 and change the list to store the following elements:

[2, 1, 36, -3, 40]

If the value of the max parameter is less than that of the min parameter you should throw an IllegalArgumentException. If the list is empty or does not contain any elements between or equal to min and max, it should return 0 and leave the list unchanged after a call to removeRange.

Obey the following restrictions in your solution:

  • The method should run in no worse than O(N) time, where N is the length of the list.
  • For full credit, you should make only one pass over the list.
  • Do not call any methods of the linked list class to solve this problem.
  • Note that the list does not have a size field, and you are not supposed to call its size method.
  • Do not use auxiliary data structures such as arrays, ArrayList, Queue, String, etc.
  • Do not modify the data field of any nodes; you must solve the problem by changing the links between nodes.
  • You may not create new ListNode objects. You may create as many ListNode variables as you like.
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.