logo Practice-It logo

minGap

Related Links:
Author: Whitaker Brand (on 2014/02/13)
  Write a method called minGap that returns the
   minimum gap between adjacent values in a list of integers.  The gap between
   two adjacent values in a list is defined as the second value minus the first
   value.  For example, suppose a variable called list stores these values:

        [1, 3, 6, 7, 12]

    The first gap is 2 (3 - 1), the second gap is 3 (6 - 3), the third gap is 1
    (7 - 6), and the fourth gap is 5 (12 - 7).  Thus, the call:

        list.minGap()

    should return 1 because that is the smallest gap.  Notice that the minimum
    gap could be a negative number.  For example, if list stores the following:

        [3, 5, 11, 4, 8]

    The gaps are 2 (5 - 3), 6 (11 - 5), -7 (4 - 11), and 4 (8 - 4).  Of these
    values, -7 is the smallest, so it would be returned.

    Your method should return 0 if the list has fewer than 2 elements.  You are
    writing a method for the LinkedIntList class:

        public class ListNode {
            public int data;       // data stored in this node
            public ListNode next;  // link to next node in the list

            <constructors>
        }
 
        public class LinkedIntList {
            private ListNode front;

            <methods>
        }

   Your method should not modify the list contents and is required to run in
   O(n) time where n is the length of the list.  You may not call any other
   methods of the LinkedIntList class and you may not construct any structured
   objects to solve this problem.
        
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.