Merge two sorted linked lists and return it as a new list.
The new list should be made by splicing together the nodes of the first two lists.
1 class Solution { 2 public: 3 ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { 4 ListNode dummy(0), *cur = &dummy; 5 while(l1 && l2) { 6 if(l1->val <= l2->val) { 7 cur->next = l1; 8 cur = cur->next; 9 l1 = l1->next; 10 } 11 else { 12 cur->next = l2; 13 cur = cur->next; 14 l2 = l2->next; 15 } 16 } 17 if(l1) cur->next = l1; 18 if(l2) cur->next = l2; 19 return dummy.next; 20 } 21 };