 <p>You have two possible options.</p> <ol> <li><p>Replace recursion with simple loop, or even non-iterative formula: <code>getLength()</code> of i-th item in the line is <code>n - i + 1</code> where <code>n</code> is the list length, assuming first item has index 1;</p></li> <li><p>[Not recommended] increase JVM stack size by adding <code>-Xss100m</code> (or any other size you consider suitable) flag to the command starts JVM;</p></li> </ol>