leetcode 合并两个有序链表 简单

leetcode 合并两个有序链表 简单

 

 

没什么好说的,很简单。也没有坑点。仔细一点就一发A

 

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode *head = new ListNode;
        auto tmp = head;
        while(l1 && l2) {
            if(l1 -> val < l2 -> val) {
                head -> next = l1;
                l1 = l1 -> next;
            }
            else {
                head -> next = l2;
                l2 = l2 -> next;
            }
            head = head -> next;
        }
        while(l1) {
            head -> next = l1;
            l1 = l1 -> next;
            head = head -> next;
        }
        while(l2) {
            head -> next = l2;
            l2 = l2 -> next;
            head = head -> next;
        }
        return tmp -> next;
    }
};

 

leetcode 合并两个有序链表 简单

上一篇:leetcode138 || 链表 || 中等


下一篇:HDU-6968(DP,和DP)