title: Java Code Review: Merge sorted lists into a single sorted list
body: <p>I want to merge sorted lists into a single list. How is this solution? I believe it runs in O(n) time. Any glaring flaws, inefficiencies, or stylistic issues?</p>
  
  <p>I don't really like the idiom of setting a flag for "this is the first iteration" and using it to make sure "lowest" has a default value. Is there a better way around that?</p>
  
  <pre><code>public static &lt;T extends Comparable&lt;? super T&gt;&gt; List&lt;T&gt; merge(Set&lt;List&lt;T&gt;&gt; lists) {
      List&lt;T&gt; result = new ArrayList&lt;T&gt;();
  
      int totalSize = 0; // every element in the set
      for (List&lt;T&gt; l : lists) {
          totalSize += l.size();
      }
  
      boolean first; //awkward
      List&lt;T&gt; lowest = lists.iterator().next(); // the list with the lowest item to add
  
      while (result.size() &lt; totalSize) { // while we still have something to add
          first = true;
  
          for (List&lt;T&gt; l : lists) {
              if (! l.isEmpty()) {
                  if (first) {
                      lowest = l;
                      first = false;
                  }
                  else if (l.get(0).compareTo(lowest.get(0)) &lt;= 0) {
                      lowest = l;
                  }
              }
          }
          result.add(lowest.get(0));
          lowest.remove(0);
      }
      return result;
  }
  </code></pre>
  
  <p>Note: this isn't homework, but it isn't for production code, either.</p>