0021.cpp (473B)
1 class Solution { 2 public: 3 ListNode *mergeTwoLists(ListNode *list1, ListNode *list2) { 4 ListNode head, *t = &head; 5 6 while (list1 && list2) { 7 if (list1->val < list2->val) { 8 t = t->next = list1; 9 list1 = list1->next; 10 } else { 11 t = t->next = list2; 12 list2 = list2->next; 13 } 14 } 15 16 t->next = list1 ? list1 : list2; 17 return head.next; 18 } 19 };