class Solution { public List<Interval> insert(List<Interval> intervals, Interval newInterval) { int idx=0; while(idx<intervals.size()&&intervals.get(idx).start<=newInterval.start) idx++; intervals.add(idx,newInterval); List<Interval> res=new ArrayList<Interval>(); int start=intervals.get(0).start; int end=intervals.get(0).end; for(int i=1;i<intervals.size();i++) { if(end<intervals.get(i).start) { res.add(new Interval(start, end)); start=intervals.get(i).start; end=intervals.get(i).end; } else end=Math.max(end, intervals.get(i).end); } res.add(new Interval(start, end)); return res; } }