Merging sorted linked lists - C++
Simplify

In this loop, the loop condition is practically useless:


while(temp1 != 0 &amp;&amp; temp2 != 0)
{
    if(temp1-&gt;data &lt;= temp2-&gt;data){
        head-&gt;next = temp1;
        head = temp1;
        if(temp1-&gt;next != NULL)
            temp1 = temp1-&gt;next;       
        else{
            head-&gt;next = temp2;
            break;
        }
    }
    else{
        head-&gt;next = temp2;
        head = temp2;
        if(temp2-&gt;next != NULL)
            temp2 = temp2-&gt;next;
        else{
            head-&gt;next = temp1;
            break;
        }
    }
}



The statements before the loop have already checked that temp1 and temp2 are not null. So the condition will be true for the first time.
Then in each cycle, you check if the next value of temp1 or temp2 will be null and if yes then break out. So you could as well change the loop condition to while (true), and the program will still work.

But instead of doing that,
it would be simpler to move those checks out of the loop body,
and let the loop condition be useful:

do {
    if (temp1-&gt;data &lt;= temp2-&gt;data) {
        head-&gt;next = temp1;
        head = temp1;
        temp1 = temp1-&gt;next;       
    } else {
        head-&gt;next = temp2;
        head = temp2;
        temp2 = temp2-&gt;next;
    }
} while (temp1 != NULL &amp;&amp; temp2 != NULL);

head-&gt;next = temp1 != NULL ? temp1 : temp2;


Taking it one step further, head can be updated outside of the if-else, like this:

do {
    if (temp1-&gt;data &lt;= temp2-&gt;data) {
        head-&gt;next = temp1;
        temp1 = temp1-&gt;next;       
    } else {
        head-&gt;next = temp2;
        temp2 = temp2-&gt;next;
    }
    head = head-&gt;next;
} while (temp1 != NULL &amp;&amp; temp2 != NULL);

head-&gt;next = temp1 != NULL ? temp1 : temp2;


You have this comment:


//temp1 should always point to head with smaller value



Nope, not really! This would work just as well:

if(headA-&gt;data &lt;= headB-&gt;data) {
  originalHead = headA;
} else { 
  originalHead = headB;
}
temp1 = headA;
temp2 = headB;


Memory management

You created a new Node for head, but you forgot to delete it.

Suggested implementation

Some further simplifications and improvements are possible:


No need to check if one of the heads are null. It's possible to rewrite using a dummy a node to handle such cases naturally without special treatment
The variable names can be improved


Implementation:

Node* MergeLists(Node *headA, Node* headB)
{
    Node *dummy = new Node();
    Node *node = dummy;

    Node *nodeA = headA;
    Node *nodeB = headB;
    while (nodeA != NULL &amp;&amp; nodeB != NULL) {
        if (nodeA-&gt;data &lt;= nodeB-&gt;data) {
            node-&gt;next = nodeA;
            nodeA = nodeA-&gt;next;
        } else {
            node-&gt;next = nodeB;
            nodeB = nodeB-&gt;next;
        }
        node = node-&gt;next;
    }

    node-&gt;next = nodeA != NULL ? nodeA : nodeB;

    Node *head = dummy-&gt;next;
    delete dummy;
    return head;
}