public ArrayList<Interval> merge(ArrayList<Interval> intervals) {
		if (intervals == null || intervals.size() == 0) {
			return intervals;
		}
		Collections.sort(intervals, new Comparator<Interval>() {

			@Override
			public int compare(Interval x, Interval y) {
				if (x.start < y.start) {
					return -1;
				} else if (x.start == y.start) {
					return x.end < y.end ? -1 : (x.end == y.end ? 0 : 1);
				} else {
					return 1;
				}
			}
		});

		ArrayList<Interval> merged = new ArrayList<Interval>();
		Interval lastInterval = null;
		for (Interval interval : intervals) {
			if (lastInterval == null) {
				lastInterval = interval;
				continue;
			}

			if (interval.start > lastInterval.end) {
				merged.add(lastInterval);
				lastInterval = interval;
			} else {
				lastInterval.end = Math.max(lastInterval.end, interval.end);
			}
		}
		merged.add(lastInterval);
		return merged;
	}