public static List<Interval> merge(List<Interval> intervals) {//AC
        if (intervals == null) return null;
        int len = intervals.size();
        if (len < 2) return intervals;

        Comparator<Interval> BY_HEAD = new Comparator<Interval> () {
            public int compare(Interval s1, Interval s2) {
                return (int)(s1.start - s2.start);
            }
        };
        Collections.sort(intervals, BY_HEAD);

        ArrayList<Interval> ret = new ArrayList<Interval>();
        int j = 0;
        int i = 0;
        while (i < len) {
            int start = intervals.get(i).start;
            int end = intervals.get(i).end;
            j = i;
            while (j < len-1 && intervals.get(j+1).start <= end) {
                if (intervals.get(j+1).end > end) {
                    end = intervals.get(j+1).end;
                }
                ++j;
            }
            ret.add(new Interval(start, end));
            i = j+1;
        }
        return ret;
    }