Can anyone help me with what mistake I am making?


#1

HERE GOES MY CODE;


/**

  • 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 ArrayList merge(ArrayList intervals) {
    if(intervals==null)
    {
    // ArrayList intervals=new ArrayList<>();
    return intervals;
    }
    else if(intervals.size()==0)
    {
    return intervals;
    }
    Interval insert= new Interval();
    int i=0;
    while(i<intervals.size()-1)
    {
    Interval current=intervals.get(i);

         Interval next=intervals.get(i+1);
         if(current.end<next.start)
         {
             ++i;
         }
             
         else
         {
             insert.start=Math.min(current.start,next.start);
             insert.end=Math.max(current.end, next.end);
             
             intervals.remove(current);
             intervals.remove(next);
             intervals.add(insert);
             i=0;
         }
     }
     return intervals;
    

    }
    }


Input:
[ (1, 10), (2, 9), (3, 8), (4, 7), (5, 6), (6, 6) ]

Output:
(5, 6)

Expected Output:
(1, 10)