/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public List<Interval> merge(List<Interval> intervals) {
Collections.sort(intervals, new Comparator<Interval>() {
public int compare(Interval i1, Interval i2) {
return i1.start - i2.start;
}
});
int index = 0;
while (index < intervals.size() - 1) {
Interval curr = intervals.get(index);
Interval next = intervals.get(index + 1);
if (next.start <= curr.end) {
curr.end = Math.max(curr.end, next.end);
intervals.remove(index + 1);
} else {
index++;
}
}
return intervals;
}
}