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 head(0); 15 ListNode *p=&head; 16 while(l1!=NULL&&l2!=NULL) 17 { 18 if(l1->val<l2->val) 19 { 20 p->next=l1; 21 p=p->next; 22 l1=l1->next; 23 } 24 else 25 { 26 p->next=l2; 27 p=p->next; 28 l2=l2->next; 29 } 30 } 31 if(l1) 32 { 33 p->next=l1; 34 } 35 if(l2) 36 { 37 p->next=l2; 38 } 39 return head.next; 40 } 41 };