title: Merging two sorted lists in C
body: <p>i have been trying to debug this code that merges two sorted lists into one sorted list im coding this problem from <strong>leetcode</strong></p>
  <pre><code>typedef struct{
     int val;
     struct ListNode *next;
  }ListNode;
   
  
  ListNode* mergeTwoLists(ListNode* l1,ListNode* l2){
          ListNode* head=(l1!=NULL||l2!=NULL)?(ListNode*)malloc(sizeof(ListNode)):NULL;
          ListNode* temp=head;
          ListNode* temp1=l1;
          ListNode* temp2=l2;
          if(temp!=NULL){
          while(temp1!=NULL&amp;&amp;temp2!=NULL){
                  temp-&gt;val=(temp1-&gt;val&lt;=temp2-&gt;val)?temp1-&gt;val:temp2-&gt;val;
                  temp-&gt;next=(ListNode*)malloc(sizeof(ListNode));
                  temp=temp-&gt;next;
                  if(temp1-&gt;val&lt;=temp2-&gt;val){
                          temp1=temp1-&gt;next;
                  }else{
                          temp2=temp2-&gt;next;
                  }
          }
          ListNode* temp3=(temp1!=NULL)?temp1:temp2;
          while(temp3!=NULL){
                  temp-&gt;val=temp3-&gt;val;
                  temp3=temp3-&gt;next;
                  if(temp3!=NULL){
                          temp-&gt;next=(ListNode*)malloc(sizeof(ListNode));
                  }else{
                          temp-&gt;next=NULL;
                  }
          }
          }
          return head;
  }
  </code></pre>
  <p>the logic seems fine to me but if we take an example<br />
  lets say we pass these two lists in
  <code>ListNode* mergeTwoLists(ListNode* l1,ListNode* l2)</code><br />
  <code>l1-&gt;(1)-&gt;(3)-&gt;(4)-&gt;NULL</code><br />
  <code>l2-&gt;(5)-&gt;(6)-&gt;(7)-&gt;NULL</code><br />
  my result : <code>head-&gt;(1)-&gt;(3)-&gt;(4)-&gt;(7)-&gt;NULL</code><br />
  what it should be : <code>head-&gt;(1)-&gt;(3)-&gt;(4)-&gt;(5)-&gt;(6)-&gt;(7)-&gt;NULL</code></p>