题目描述

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 lists.

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

参考答案

 /**
* 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 res(); // content
ListNode* cur = &res; // pointer cur = taking addree of res
// *content. &pointer-> while(l1&&l2){
if(l1->val > l2 ->val){
cur->next = l2;
l2 = l2->next;
}else{
cur->next = l1;
l1 = l1->next;
}
cur = cur->next;
}
cur->next = l2?l2:l1;
return res.next;
}
};

答案解析

新建一个空节点,不是地址,而是内容,然后将该内容对应的地址,附给一个cur指针。

进行loop

返回的时候,因为对于struct而言,提取成员时,内容使用 点,指针使用 -> 。

LC 21. Merge Two Sorted Lists的更多相关文章

  1. [Leetcode][Python]21: Merge Two Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...

  2. 21. Merge Two Sorted Lists(合并2个有序链表)

    21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...

  3. 21. Merge Two Sorted Lists【easy】

    21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...

  4. 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists

    21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...

  5. leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)

    1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...

  6. 刷题21. Merge Two Sorted Lists

    一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...

  7. C# 写 LeetCode easy #21 Merge Two Sorted Lists

    21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...

  8. [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 t ...

  9. [leetcode] 21. Merge Two Sorted Lists (Easy)

    合并链表 Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists. class ...

随机推荐

  1. MySQL 聚集拼接

    GROUP_CONCAT()函数 示例: 假设现在有这样一个表结构: 其中`student`.`school_id`是逻辑外键 想要检索出所有学校,其中学校下的学生名需要拼接在一起,作为结果集的字段 ...

  2. Mac佳软之Understand---Android源码分析阅读神器

    下载地址, 密码:gebp 供大家体验, 请大家支持正版!!! https://www.jianshu.com/p/06f25d9131de

  3. Linux中的MySQL授权远程连接

    Linux中 MySQL 授权远程连接 参考地址:https://www.centos.bz/2018/10/linux%e4%b8%ad-mysql-%e6%8e%88%e6%9d%83%e8%bf ...

  4. sqlmap环境搭建

    1.安装Python2.7.12 1.1.下载Python2.7.12地址:https://www.python.org/downloads/ 1.2.环境变量配置Python2.7.11 1.3.验 ...

  5. 数据库连接池配置(案例及排查指南) 原创: 有赞技术 有赞coder 4天前

    数据库连接池配置(案例及排查指南) 原创: 有赞技术 有赞coder 4天前

  6. 远程控制软件 mRemoteNG,管理多台虚拟机

    #下载 1.官网下载:https://mremoteng.org/2.当然也可以到这里下载:https://pan.baidu.com/s/11O_QNM3HudN3IyTiqqHmrQ,提取码:jm ...

  7. mac安装 bcolz出现错误

    使用的是命令pip install bcolz c-blosc//snappy-stubs-:: fatal error: 'algorithm' file not found #include &l ...

  8. ARM程序的RO段、RW段和ZI段 --Image

    Limit 含义了解RO,RW和ZI需要首先了解以下知识:ARM程序的组成此处所说的“ARM程序”是指在ARM系统中正在执行的程序,而非保存在ROM中的bin映像(image)文件,这一点清注意区别. ...

  9. IFC数据模型构件控制

    控制ifc构件的隐藏与显示.着色 osg::ref_ptr<osg::Geode> geode1 = new osg::Geode(); osg::ref_ptr<osg::Stat ...

  10. Qps 和 tps的解释

    QPS:Queries Per Second意思是“每秒查询率”,是一台服务器每秒能够相应的查询次数,是对一个特定的查询服务器在规定时间内所处理流量多少的衡量标准. TPS:是Transactions ...