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.

/**
* 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 == NULL) return l2;
if(l2 == NULL) return l1; ListNode* root = l1;
if(l1->val <= l2->val){
root = l1;
l1 = l1->next;
}
else{
root = l2;
l2 = l2->next;
} ListNode* current = root;
while(l1 && l2){
if(l1->val <= l2->val){
current->next = l1;
l1 = l1->next;
}
else{
current->next = l2;
l2 = l2->next;
}
current = current->next;
} if(l1){
current->next = l1;
}
else if(l2){
current->next = l2;
} return root;
}
};

21.Merge Two Sorted Lists (List)的更多相关文章

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

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

随机推荐

  1. C# #if, #else和#endif预处理指令

        #if 使您可以开始条件指令,测试一个或多个符号以查看它们是否计算为 true.如果它们的计算结果确实为true,则编译器将计算位于 #if 与最近的 #endif 指令之间的所有代码.例如, ...

  2. 博客(第0次作业)—— New Starting Point

    一.最理想的师生关系是健身教练和学员的关系,在这种关系中你期望获得来自老师的那些帮助? 正如文章中所说,这些学员的想法得足够强烈, 他/她才会花钱去参加这样的健身活动,每一个来学习的学生,  都是想学 ...

  3. ubuntu 通过ssh上传/下载服务器文件

    1.用ssh登录远程ubuntu主机 (主机ip为:1.2.3.4;用户名:username) ssh username@1.2.3.4 2.从远程ubuntu主机copy文件/文件夹到本地(scp) ...

  4. IDEA 使用generator逆向工程生成pojo,mapper

    1.新建立一个MAVEN项目 2.在pom.xml增加配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns: ...

  5. flow 编写flow-typed 定义(官方文档)

    此为官方文档,因为墙的问题,记录下来: Before spending the time to write your own libdef, we recommend that you look to ...

  6. celery docker 基本使用

    项目参考官网资料,比较简单的add task 具体代码参考https://github.com/rongfengliang/celery-docker-demo 项目结构 ├── README.md ...

  7. eclipse中点不出来提示

    当在用eclipse或是myeclipse时,可能会遇到不能自动提示,就是当你用到点的时候,后面不会出现相关的提示信息.这时,解决方法如下 : 1.菜单window->Preferences-& ...

  8. stacks and queues--codility

    lesson 7: stacks and queues 1. Nesting 2. StoneWall 3. Brackets 4. Finsh lesson 7: stacks and queues ...

  9. wamp 配置多站点访问

    1:在f:\wamp\bin\apache\apache2.2.21\conf目录下打开 httpd.conf 查找到 #include conf/extra/httpd-vhosts.conf 把前 ...

  10. Java 字符串与对象进行比较 compareTo()

    Java 手册 compareTo public int compareTo(String anotherString) 按字典顺序比较两个字符串.该比较基于字符串中各个字符的 Unicode 值.按 ...