mythjill
BAN USER/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
struct ListNode* head;
struct ListNode* cur;
if (!l1) {
return l2;
} else if(!l2) {
return l1;
}
if (l1->val < l2->val) {
head = l1;
l1 = l1->next;
} else {
head = l2;
l2 = l2->next;
}
cur = head;
while(true) {
if (l1 && l2) {
if (l1->val <l2->val) {
cur->next = l1;
l1 = l1->next;
} else {
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
} else if (!l1 && !l2) {
break;
} else if (l1 && !l2) {
cur->next = l1;
break;
} else if (!l1 && l2) {
cur->next = l2;
break;
}
}
return head;
}
- mythjill May 12, 2016