Leetcode 21. Merge Two Sorted Lists

Posted randyniu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 21. Merge Two Sorted Lists相关的知识,希望对你有一定的参考价值。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode temp_head(0);
        ListNode * pre = &temp_head;
        while(l1 && l2)
        {
            if(l1->val < l2->val)
            {
                pre->next = l1;
                l1 = l1->next;
            }
            else
            {
                pre->next = l2;
                l2 = l2->next;
            }
            pre = pre->next;
        }
        if(l1)
        {
            pre->next = l1;
        }
        if(l2)
        {
            pre->next = l2;
        }
        return temp_head.next;
    }
};

 

以上是关于Leetcode 21. Merge Two Sorted Lists的主要内容,如果未能解决你的问题,请参考以下文章