Free AI web copilot to create summaries, insights and extended knowledge, download it at here
2191
Abstract
e <span class="hljs-keyword">Result</span> LinkedList <span class="hljs-keyword">in</span> Head node.
( ListNode head = <span class="hljs-keyword">result</span><span class="hljs-punctuation">;</span> )
<span class="hljs-number">3</span>: <span class="hljs-keyword">While</span> ( l1 <span class="hljs-keyword">and</span> l2 <span class="hljs-keyword">not</span> null )
<span class="hljs-keyword">then</span> we compare
l1.val <span class="hljs-keyword">is</span> less than l2.val
<span class="hljs-keyword">result</span>.next = l1
l1=l1.next
<span class="hljs-keyword">if</span> l1.val <span class="hljs-keyword">is</span> equal <span class="hljs-keyword">or</span> greater than l2.val
<span class="hljs-keyword">result</span>.next = l2<span class="hljs-punctuation">;</span>
l2=l2.next<span class="hljs-punctuation">;</span>
<span class="hljs-keyword">result</span> = <span class="hljs-keyword">result</span>.next<span class="hljs-punctuation">;</span>
<span class="hljs-number">4</span>: <span class="hljs-keyword">if</span> <span class="hljs-keyword">while</span> <span class="hljs-keyword">loop</span> terminated <span class="hljs-keyword">then</span> , either l1 <span class="hljs-keyword">is</span> finished traversing <span class="hljs-keyword">or</span> l2 <span class="hljs-keyword">is</span> finished traversing.
<span class="hljs-keyword">if</span> l1 <span class="hljs-keyword">is</span> finished traversing <span class="hljs-keyword">then</span>
<span class="hljs-keyword">result</span>.next = l2</pre></div><div id="6f39"><pre> else
result.next <span class="hljs-operator">=</span> l2<span class="hljs-comment">;</span></pre></div><div id="934c"><pre><span class="hljs-number">5</span>: <span class="hljs-keyword">return</span><span class="hljs-built_in"> head</span>.<span class="hljs-keyword">next</span> // <span class="hljs-keyword">return</span><span class="hljs-built_in"> head</span> <span class="hljs-keyword">of</span> the result</pre></div><h1 id="d0ac">Solutions</h1>
<figure id="b87a">
<div>
<div>
<iframe class="g
Options
ist-iframe" src="/gist/imsurajmishra/8a3e09f28bc672edf837a776c1be0e78.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><h2 id="e8f2">Result</h2><p id="ee91">Our solution passes all the test cases and it’s accepted by Leetcode & Our code runtime is 0 ms which is great.</p><figure id="b551"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*GBKSMWur9S0R28w2I098yg.png"><figcaption></figcaption></figure><h2 id="5ca8">Conclusion</h2><p id="39cb">This Leetcode problem is a good start to playing with LinkedList data structure and getting confident with it. We can as the next step target more difficult <a href="https://javarevisited.blogspot.com/2017/07/top-10-linked-list-coding-questions-and.html#axzz6fY0boe26">LinkedList questions</a>.</p><p id="0e50">Let me know if you have some opinions on the solution in the comment section!</p><h2 id="e4bc">Bonus</h2><ul><li>If you want to upskill your coding interview game, you should definitely check out <a href="https://click.linksynergy.com/link?id=FAaRt1BJn8w&offerid=1060092.1419186&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Fcourse%2Fdata-structures-and-algorithms-deep-dive-using-java%2F"><b><i>this bestseller course</i></b></a> ( this is in Java )</li></ul><p id="481f">Happy Leetcoding!</p><blockquote id="fbe1"><p>Let’s connect on <a href="https://www.linkedin.com/in/suraj-mishra-16b515a4"><b>LinkedIn</b></a><b>
</b>If you like this article, please check more @ <a href="https://asyncq.com/"><b>https://asyncq.com/</b></a></p></blockquote><h2 id="7c02">Other Leetcode Problems</h2><ul><li><a href="https://mishrasuraj.medium.com/solve-with-me-leetcode-problem-9-1666a577bddd">Leetcode Problem #9</a></li><li><a href="https://mishrasuraj.medium.com/solve-with-me-leetcode-problem-13-df876bd0e2b1">Leetcode Problem #13</a></li><li><a href="https://mishrasuraj.medium.com/solve-with-me-leetcode-problem-20-3f43dc8a914f">Leetcode Problem #20</a></li><li><a href="https://mishrasuraj.medium.com/solve-with-me-leetcode-problem-7-1d362bb441e7">Leetcode Problem #7</a></li></ul></article></body>