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* merge(ListNode* l1, ListNode* l2) {
    12. ListNode *dummy = new ListNode(0);
    13. ListNode *cur = dummy;
    14. while(l1 != NULL && l2 != NULL){
    15. if(l1->val < l2->val){
    16. cur->next = l1;
    17. l1 = l1->next;
    18. }else{
    19. cur->next = l2;
    20. l2 = l2->next;
    21. }
    22. cur = cur->next;
    23. }
    24. cur->next = (l1 != NULL ? l1 : l2);
    25. return dummy->next;
    26. }
    27. };