public static ArrayList<Interval> insert(ArrayList<Interval> intervals,
			Interval newInterval) {
		ArrayList<Interval> list = new ArrayList<Interval>();
		boolean newIntervalProcessed = false;
		for (Interval interval : intervals) {
			if (newIntervalProcessed) {
				list.add(interval);
				continue;
			}

			if (interval.start > newInterval.end) {
				if (!newIntervalProcessed) {
					list.add(newInterval);
					newIntervalProcessed = true;
				}
				list.add(interval);
			} else if (newInterval.start <= interval.start) {
				newInterval = new Interval(newInterval.start, Math.max(
						newInterval.end, interval.end));
			} else if (newInterval.start <= interval.end) {
				newInterval = new Interval(interval.start, Math.max(
						newInterval.end, interval.end));
			} else {
				list.add(interval);
			}
		}

		if (!newIntervalProcessed) {
			list.add(newInterval);
		}
		return list;
	}