avatarSuraj Mishra

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&amp;offerid=1060092.1419186&amp;type=2&amp;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>

Merge Sorted List (Solution For Leetcode Problem #21)

Java Solution for https://leetcode.com/problems/merge-two-sorted-lists/

Originally published at https://asyncq.com

Youtube Video

If you prefer video format: https://www.youtube.com/watch?v=29RWY7bEq-g

Understanding Problem

  • We have been given two sorted singly linked lists and our job is to merge these two lists such that the result list should also be sorted.
  • For example
l1 = [2,3,4,5] & l2=[1,6,7,8]
result = [1,2,3,4,5,6,7,8]

My Thought About Solution

  • The solution for this problem looks simple, we can traverse both the list and compare the node value, and based on it we can sort and write the result to result_list.
  • Data structure: We need LinkedList to store sorted values from the input list Time-complexity: O(N or M) where N & M is the length of the is the input list, and traversal would end at whichever list we choose as the first list for comparison Space-complexity: O(N+M), since the result list will hold all the input elements from the first and second LinkedList

Pseudocode

1: Create Result LinkedList
2: Store start of the Result LinkedList in Head node. 
   ( ListNode head = result; )
3: While ( l1 and l2 not null )
   then we compare 
      l1.val is less than l2.val 
        result.next = l1
        l1=l1.next
      if l1.val is equal or greater than l2.val
        result.next = l2;
        l2=l2.next;
      
     result = result.next;
4: if while loop terminated then , either l1 is finished traversing or l2 is finished traversing.
   if l1 is finished traversing then
     result.next = l2
   else 
     result.next = l2;
5: return head.next     // return head of the result

Solutions

Result

Our solution passes all the test cases and it’s accepted by Leetcode & Our code runtime is 0 ms which is great.

Conclusion

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 LinkedList questions.

Let me know if you have some opinions on the solution in the comment section!

Bonus

  • If you want to upskill your coding interview game, you should definitely check out this bestseller course ( this is in Java )

Happy Leetcoding!

Let’s connect on LinkedIn If you like this article, please check more @ https://asyncq.com/

Other Leetcode Problems

Leetcode
Leetcode Easy
Data Structures
Singly Linked List
Sorting Algorithms
Recommended from ReadMedium