Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could do the next steps: get the LinkedList of Java source here: <a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/LinkedList.java" rel="nofollow">LinkedList.java</a></p> <p>Then over this implementation add the next function:</p> <pre><code>public void concatenate(LinkedList&lt;E&gt; list) { header.previous.next = list.header.next; list.header.next.previous = header.previous; list.header.previous.next = header.next; header.next.previous = list.header.previous; list.header.next = header.next; header.previous = list.header.previous; size = size + list.size; modCount = modCount + list.modCount + 1; list.size = size; list.modCount = modCount; } </code></pre> <p>With this code, the 2 LinkedList will be the same LinkedList, so you'll merge in one. The container LinkedList will add the param LinkedList at the end and finally the header of both LinkedList will point to the first and last element. In this method I dont care about if one of the two list is empty so make sure you have the two list with elements before use it or you'll have to check and take care about this.</p> <p>Test1:</p> <pre><code>public static void main(String[] args) { LinkedList&lt;String&gt; test1 = new LinkedList&lt;String&gt;(); LinkedList&lt;String&gt; test2 = new LinkedList&lt;String&gt;(); test1.add("s1"); test1.add("s2"); test2.add("s4"); test2.add("s5"); test1.concatenate(test2); System.out.println(test1); System.out.println(test2); } </code></pre> <p>out:</p> <pre><code>[s1, s2, s4, s5] [s1, s2, s4, s5] </code></pre> <p>Test2 performance:</p> <pre><code>public static void main(String[] args) { int count = 100000; myutil.LinkedList&lt;String&gt; test1 = new myutil.LinkedListExt&lt;&gt;(); myutil.LinkedList&lt;String&gt; test2 = new myutil.LinkedListExt&lt;&gt;(); test1.add("s1"); test1.add("s2"); test2.add("s3"); test2.add("s4"); for (int i=0; i&lt;count; ++i) test2.add("s"); long start = System.nanoTime(); test1.concatenate(test2); long elapsedTime = System.nanoTime() - start; System.out.println(elapsedTime/1000000.0); java.util.LinkedList&lt;String&gt; test3 = new java.util.LinkedList&lt;&gt;(); java.util.LinkedList&lt;String&gt; test4 = new java.util.LinkedList&lt;&gt;(); test3.add("s1"); test3.add("s2"); test4.add("s3"); test4.add("s4"); for (int i=0; i&lt;count; ++i) test4.add("s"); start = System.nanoTime(); test3.addAll(test4); elapsedTime = System.nanoTime() - start; System.out.println(elapsedTime/1000000.0); } </code></pre> <p>out:</p> <pre><code>0.004016 10.508312 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload