title: Merging a 2D array of intervals
body: <p>Below is code that takes a 2D array (a list of intervals) and merges them. Each interval is of size 2, but the list of intervals is of size <code>n</code>, e.g.</p>
  <pre><code>intervals = [[1,2], [2,4], [8,10]]
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>public int[][] merge(int[][] intervals) {
      Arrays.sort(intervals, (a, b) -&gt; Integer.compare(a[0], b[0]));
      LinkedList&lt;int[]&gt; mergedList = new LinkedList&lt;int[]&gt;();
  
      for (int i = 0; i &lt; intervals.length; i++) {
          if (mergedList.size() == 0 || mergedList.getLast()[1] &lt; intervals[i][0]) {
              mergedList.add(intervals[i]);
          } else {
              int max = Math.max(mergedList.getLast()[1], intervals[i][1]);
              mergedList.getLast()[1] = max;
              // mergedList.getLast() = new int[] { mergedList.getLast()[0], max };
          }
      }
      return mergedList.toArray(new int[mergedList.size()][]);
  }
  </code></pre>
  <p>In the else statement, I originally tried the commented line but it gave me an unexpected type error. Why am I unable to replace the array within the mergedList BUT I'm able to replace the value within it? And when replacing the value, how do I know it's not just creating a copy and actually modifying the linked list?</p>