# This is my solution

• ``````ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
if (l1 == NULL) return l2;
if (l2 == NULL) return l1;

if (l1->val < l2->val) { small = l1; big = l2; }
else { small = l2; big = l1; }

while (small != NULL && big != NULL) {
if (small->next == NULL || small->next->val > big->val) {
next = small->next;
small->next = big;
small = big;
big = next;
} else {
small = small->next;
}
}

}``````

• This post is deleted!

• This post is deleted!

• Although program is a little bit obscure, it is refined when carefully looking.

• Refined code:

``````class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
ListNode *aux = new ListNode(0);
ListNode *tmp = aux;

while(l2) {
if (l1 == NULL) {
l1 = l2;
break;
}
else if(l1->val < l2->val) {
tmp = tmp->next = l1;
l1 = l1->next;
}
else {
tmp = tmp->next= l2;
l2 = l2->next;
}
}

tmp->next = l1;
tmp = aux->next;
delete aux;

return tmp;
}
};
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.