要定义两个链表

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

/**
* 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. fast-ai lesson1 错误处理(CNN创建)

    报错信息: name 'ConvLearner' is not defined 在最新的fast ai包中,ConvLearner已经被create_cnn取代,所以替换为下列语句就好了: learn ...

  2. 使用ubuntu做为dotnet core开发环境

    一.安装google浏览器 1.下载安装包(传送门:http://www.google.cn/intl/zh-CN/chrome/browser/desktop/index.html) 2.使用sud ...

  3. CF1129B 【Wrong Answer】

    既然 $ n \leq 2000$ 那我们就假使所有的 $n = 2000 $ 主要是为了方便.再使 \(x = \sum_{i=1} ^ {1999}\) 以及 $a_1=a_2=a_3=...=a ...

  4. 【强大的PDF格式转换工具】Lighten PDF Converter OCR for Mac 6.2.0

    [简介] Lighten PDF Converter OCR 是一款Mac上强大的PDF格式转换工具,可以将PDF文档快速批量的转换为Office (Word, Excel, PowerPoint), ...

  5. 编写高质量的Python代码系列(三)之类与继承

    用Python进行编程时,通常需要编写心累,并定义这些类应该如何通过其接口及继承体系与外界交互.本节讲解如何使用类和继承来表达对象所以更具备的行为. 第二十二条:尽量用辅助类来维护程序的状态,而不要用 ...

  6. ClassLoader那事儿

    ClassLoader是什么 ClassLoader中文类加载器,java编写出来的是.java文件,然后编译成.class文件,而ClassLoader就是把class文件加载到jvm内存中:但jv ...

  7. 洛谷红名+AC150祭

    emmmm没什么想说的,随便放个图吧23333(逃~

  8. python3 练手实例5 做一个简单电子时钟

    import time,sys,os while(1): t = time.strftime('%Y-%m-%d\n%H:%M:%S',time.localtime(time.time())) pri ...

  9. HDU-1171 Big Event in HDU(生成函数/背包dp)

    题意 给出物品种类,物品单价,每种物品的数量,尽可能把其分成价值相等的两部分. 思路 背包的思路显然是用一半总价值当作背包容量. 生成函数则是构造形如$1+x^{w[i]}+x^{2*w[i]}+.. ...

  10. js数字串传参时变科学计数法

    例1:onclick=channel_info_listFt(\"'+val.gid+'\",'+val.deviceIdOwner+','+val.gname+') 当长度过长的 ...