Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Probably just me, but I don't like any of the solutions provided.</p> <pre><code> public static LinkedList Of(String[] input) { if (input == null || input.length &lt; 1) return null; return LinkedList.Of(input, input.length - 1); } public static LinkedList Of(String[] input, int i) { if (i &gt; 0) return new LinkedList(input[i], LinkedList.Of(input, i - 1)); return new LinkedList(input[i], null); } </code></pre> <p>Down vote is scary, so here is it in the right order:</p> <pre><code> /** * Creates linked list from array input. * * @param input * data array * @return linked list with data */ public static LinkedList Of(String[] input) { // Checks if array has elements. if (input == null || input.length &lt; 1) return null; // Starts creating the array using overload 2. return LinkedList.Of(input, 0); } /** * Creates linked list from array input (overload 2). * * @param input * data array * @param i * counter to remember at what element is current * @return linked list with data */ public static LinkedList Of(String[] input, int i) { //Tests if counter is within array elements. if (input.length - 1 &gt; i) // Returns new element with (current element data, reference // to next element). Note that next element will be returned // by this same method (this is why it is recursive). return new LinkedList(input[i], LinkedList.Of(input, i + 1)); //Last element. From here backtracking will begin. return new LinkedList(input[i], null); } </code></pre> <p>Here is something for up vote:</p> <pre><code> public String toString() { StringBuilder sb = new StringBuilder(this.word); LinkedList tmp = this; while (tmp.next != null) { sb.append(" &gt; "); tmp = tmp.next; if (tmp.word != null) sb.append(tmp.word); } return sb.toString(); } </code></pre> <p>And to test:</p> <pre><code> String str = "Neque porro quisquam est qui dolorem ipsum quia " + "dolor sit amet, consectetur, adipisci velit..."; LinkedList ll = LinkedList.Of(str.split("\\s+")); System.out.println(ll); </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