Is this code is accepted at original coding round

programming
Tags: #<Tag:0x00007f242873fcf0>

#1

class Sortbystart implements Comparator
{
// Used for sorting in ascending order of
// roll number
public int compare(Interval a, Interval b)
{
return a.start - b.start;
}
}
public class Solution {
public ArrayList merge(ArrayList intervals) {
Collections.sort(intervals,new Sortbystart());
/* for(Interval e: intervals)
System.out.println("br "+"start “+e.start+” end "+e.end);*/
int i=0;
Interval current,next;
if(intervals.size()==1)
return intervals;
else{
while(i<intervals.size())
{

            current = intervals.get(i);
            int j=i+1;
            if(j==intervals.size())
            break;
            next=intervals.get(j);
            Interval toInsert = new Interval();
            if(current.end>next.start)
            {
               // System.out.println(i+" entered "+ j);
                toInsert.start=Math.min(current.start,next.start);
               // System.out.println(toInsert.start+" start ");
                toInsert.end=Math.max(current.end,next.end);
               // System.out.println(toInsert.end+" end ");
                intervals.remove(current);
                intervals.remove(next);
                
                
                /*for(Interval e: intervals)
                   System.out.println("br "+"start "+e.start+" end "+e.end);*/
                intervals.add(i,toInsert);
               /* for(Interval e: intervals)
                System.out.println("ar " +"start "+e.start+" end "+e.end);*/
            }else if(current.end>=next.start){
                if(current.end<=next.end){
                    toInsert.start=Math.min(current.start,next.start);
               // System.out.println(toInsert.start+" start ");
                toInsert.end=Math.max(current.end,next.end);
               // System.out.println(toInsert.end+" end ");
                intervals.remove(current);
                intervals.remove(next);
                
                
                /*for(Interval e: intervals)
                   System.out.println("br "+"start "+e.start+" end "+e.end);*/
                intervals.add(i,toInsert);
                }
            }
            
            else{
                i++;
            }
        }
        return intervals;
        }

}

}