1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { 12 // Start typing your C/C++ solution below 13 // DO NOT write int main() function 14 ListNode *p = l1, *q = l2, dummy(-1); 15 ListNode *curr = &dummy; 16 while (p || q) { 17 int a = INT_MAX, b = INT_MAX; 18 if (p) { 19 a = p->val; 20 } 21 if (q) { 22 b = q->val; 23 } 24 if (a < b) { 25 curr->next = p; 26 p = p->next; 27 } 28 else { 29 curr->next = q; 30 q = q->next; 31 } 32 curr = curr->next; 33 } 34 return dummy.next; 35 } 36 };