public ArrayList<Interval> merge2(ArrayList<Interval> intervals) {

    ArrayList<Interval> result = new ArrayList<Interval>();

    for (Interval newInterval : intervals) {

      boolean isNew = true;

      for (Interval existingInterval : result) {

        if (isOverlap(newInterval, existingInterval)) {
          existingInterval.start = (existingInterval.start < newInterval.start ? existingInterval.start : newInterval.start);
          existingInterval.end = (existingInterval.end > newInterval.end ? existingInterval.end : newInterval.end);

          isNew = false;
          break;
        }

      }

      if (isNew) {
        Interval inteval = new Interval(newInterval.start, newInterval.end);
        result.add(inteval);
      }
    }

    return result;
  }