title: Some confusion in a solution to Leetcode NQueen
body: <pre><code>public class Solution {
  
  Set&lt;Integer&gt; column = new HashSet&lt;Integer&gt;();
  Set&lt;Integer&gt; diag1 = new HashSet&lt;Integer&gt;();
  Set&lt;Integer&gt; diag2 = new HashSet&lt;Integer&gt;();
  
  public List&lt;List&lt;String&gt;&gt; solveNQueens(int n) {
      ArrayList&lt;List&lt;String&gt;&gt; result = new ArrayList&lt;List&lt;String&gt;&gt;();
      ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
      dfs(result, list, 0, n);
      return result;
  }
  
  private void dfs(ArrayList&lt;List&lt;String&gt;&gt; result, ArrayList&lt;String&gt; list,
          int row, int n) {
      if (row == n) {
          result.add(new ArrayList&lt;String&gt;(list)); 
          // Why I have to "result.add(new ArrayList&lt;String&gt;(list))"?
          // Why can't I just "result.add(list)"?
  
          return;
      }
  
      for (int j = 0; j &lt; n; j++) {
          if (column.contains(j) || diag1.contains(row + j)
                  || diag2.contains(row - j))// check if there are other
                                              // queens in the same column,
                                              // diagonal1 or diagonal2
              continue;
  
  
          char[] rowChar = new char[n];
          Arrays.fill(rowChar, '.');
          rowChar[j] = 'Q';
          String str = new String(rowChar);
  
          list.add(str);
          column.add(j);
          diag1.add(row + j);
          diag2.add(row - j);
  
          dfs(result, list, row + 1, n);
  
          // remove the queen and the constraints
          list.remove(list.size() - 1);
          column.remove(j);
          diag1.remove(row + j);
          diag2.remove(row - j);
      }
  
  }
  </code></pre>
  
  <p>It's an N-Queens problem in Leetcode. I found a solution to the problem but I have a question which I have commented in the code. 
  Why I have to "result.add(new ArrayList(list))"?
  Why can't I just "result.add(list)"? 
  Since when I use "result.add(list)", the result is not correct. What's wrong with it?</p>