要定义两个链表

判断时依次对应每一个链表的值进行判断即可。

/**
* 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* h;
ListNode* l3=new ListNode(); //注意
h=l3; //注意
while(l1!=NULL&&l2!=NULL){
if(l1->val<l2->val){
h->next=l1;
l1=l1->next;
}
else{
h->next=l2;
l2=l2->next;
}
h=h->next;
}
if(l1!=NULL){
h->next=l1;
}
if(l2!=NULL){
h->next=l2;
}
return l3->next;
}
};

LeetCode 21. Merge Two Sorted Lists(c++)的更多相关文章

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

  2. Leetcode 21. Merge Two Sorted Lists(easy)

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  3. LeetCode 21 Merge Two Sorted Lists (有序两个链表整合)

    题目链接 https://leetcode.com/problems/merge-two-sorted-lists/?tab=Description   Problem: 已知两个有序链表(链表中的数 ...

  4. LeetCode 21. Merge Two Sorted Lists(合并两个有序链表)

    题意:合并两个有序链表 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next ...

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

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

  8. LeetCode 23 Merge k Sorted Lists(合并k个有序链表)

    题目链接: https://leetcode.com/problems/merge-k-sorted-lists/?tab=Description Problem: 给出k个有序的list, 将其进行 ...

  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. H5键盘事件处理

    if (/Android/gi.test(navigator.userAgent)) { const innerHeight = window.innerHeight; window.addEvent ...

  2. 源码分析 ucosii/source 任务源码详细分析

    分析源码: 得先学会读文档, 函数前边的 note :是了解该程序员的思想的途径.不得不重视 代码前边的  Notes,了解思想后,然后在分析代码时看他是如何具体实现的. 1. ucosii/sour ...

  3. Luogu4495 [HAOI2018] 奇怪的背包 【扩展欧几里得算法】

    题目分析: 首先打个暴力求一下$10^9$以内因子最多的数的因子个数,发现只有$1344$个. 由于有$ax+by=k*(a,b)$和2017年noip的结论,所以我们可以发现对于任意多个数$a_1, ...

  4. Shell 简单构建 Node web服务器

    .git bash 执行代码生成: ./makeJs.sh 生成文件如下: 访问:http://127.0.0.1:3030/index.html makeJs.sh  代码如下: #create m ...

  5. @WebFilter注解

    @WebFilter @WebFilter 用于将一个类声明为==过滤器==,该注解将会在部署时被容器处理,容器将根据具体的属性配置将相应的类部署为过滤器.该注解具有下表给出的一些常用属性 ( 以下所 ...

  6. ubuntu下adb的使用以及开启黑域

    ubuntu使用adb开启黑域 刷了原生后经好友推荐, 黑域对于App的管控效果还是很不错的 adb的安装 此处顺带着就把fastboot也安装了 sudo apt update sudo apt i ...

  7. 2018-2019 2 20175230《Java程序设计》第九周学习总结

    <Java程序设计>第九周学习总结 主要内容 MySQL数据库管理系统 1.下载 2.安装 启动MySQL数据库服务器 1.启动 2.root用户 MySQL客户端管理工具 建立连接 建立 ...

  8. python1--计算机原理 操作系统 进制 内存分布

    本周内容   '''第一天: 计算机原理 操作系统  第二天: 编程语言 python入门:环境 - 编辑器 变量 基本数据类型 '''``` ## 学习方法 ```python'''鸡汤 - 干货 ...

  9. Maven(一)简介安装

    一.什么是maven,它能干什么? ①:maven maven是一个项目管理工具.它包含了一个项目对象模型,一组标准集合,一个生命周期,一个依赖管理系统,和用来运行定义生命周期中插件目标的逻辑. 传统 ...

  10. SQL 农经权数据库问题提取_身份证号码相同(字段值出现多次);身份证号码相同但姓名不同(A字段相同,B字段不相同);发包方无承包方信息(A表有,B表无)等

    身份证号码相同(字段值出现多次) select * from CBF_JTCY a,(select CYZJHM, count(*) from CBF_JTCY  group by  CYZJHM h ...