title: This is the question of merging two sorted lists. I could understand where is the problem?
body: <p>The program accepts two sorted linked lists and returns one linked list by merging these linked lists. I have written the logic considering edge cases too but the output is not coming. I think the problem lies in the formation of a third linked list. I am unable to form the third linked list.</p>
  
  <pre><code>class Solution {
  public:
      ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
          ListNode *l3 = new ListNode(0);
          ListNode *head = l3;
          ListNode *temp = new ListNode();
          if (!l1 &amp;&amp; !l2) {
              return l1;
          } else if (!l1) {
              return l2;
          } else if (!l2) {
              return l1;
          } else if (l1-&gt;val &lt;= l2-&gt;val) {
              l3-&gt;val = l1-&gt;val;
              l1 = l1-&gt;next;
          } else {
              l3-&gt;val = l2-&gt;val;
              l2 = l2-&gt;next;
          }
          while (l1-&gt;next || l2-&gt;next) {
              print(head);
              print(l1);
              print(l2);
              if (l1-&gt;val &lt;= l2-&gt;val) {
                  temp-&gt;val = l1-&gt;val;
                  l1 = l1-&gt;next;
              } else {
                  temp-&gt;val = l2-&gt;val;
                  l2 = l2-&gt;next;
             }
              print(temp);
              //l3 = l3-&gt;next;
              l3-&gt;next = temp;
              l3 = l3-&gt;next;
          }
          return head;
      }
  };
  </code></pre>