class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1 == NULL && l2 == NULL)
return l1;
if(l1 == NULL)
return l2;
if(l2 == NULL)
return l1;
ListNode *hd= NULL;
ListNode *cr= NULL;
int x;
int y;
while(l1 != NULL && l2 != NULL ){
x = l1 ? l1->val:0;
y = l2 ? l2->val:0;
if (hd == NULL) {
if (x <= y){
cr = new ListNode(x);
l1 = l1 ? l1->next:NULL;
}
else{
cr = new ListNode(y);
l2 = l2 ? l2->next:NULL;
}
hd = cr;
}
else{
if (x <= y){
cr->next = new ListNode(x);
l1 = l1 ? l1->next:NULL;
}
else{
cr->next = new ListNode(y);
l2 = l2 ? l2->next:NULL;
}
cr = cr->next;
}
}
if(l1 != NULL ){
cr->next = l1;
}
if(l2 != NULL ){
cr->next = l2;
}
return hd;
}
};