【LeetCode】21. Merge Two Sorted Lists 解题小结


题目:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lis

相对还是简单的,直接合并。

/**
 * 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) {
        if (!l1 || !l2) return l1? l1:l2;
        ListNode* head = new ListNode(0);
        ListNode* node = head;
        while (l1 && l2){
            if (l1->val < l2->val){
                node->next = l1;
                l1 = l1->next;
            }
            else {
                node->next = l2;
                l2 = l2->next;
            }
            node = node->next;
        }
        node->next = l1?l1:l2;
        return head->next;
    }
};

优质内容筛选与推荐>>
1、Python3 学习第六弹: 迭代器与生成器
2、mhp3usb联机
3、java中静态代码块的用法
4、saxbuilder用法
5、MySQL状态变量Aborted_connects与Aborted_clients浅析


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号