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. 关于React setState的实现原理(一)

    前言 首先在学习react的时候就对setSate的实现有比较浓厚的兴趣,那么对于下边的代码,可以快速回答吗? class Root extends React.Component { constru ...

  2. window XP下 php5.5+mysql+apache2+phpmyadmin安装

    学了将近大半年的php了,还没有装过独立的php环境,一直用的集成的.记得刚学时,一头雾水,不知改怎么搭配环境,就觉得特别复杂,各种看不懂,今天还是自己在XP环境下搭配了一个. 首先,下载php5.5 ...

  3. UVA-11478 Halum【二分】【差分约束】

    LINK1 LINK2 题目大意 给你一个n个点,m条边的有向图 有一种操作把所有到达这个点的边全部减小d,把所有从从这个点出发的边加上d 问最后是否可以让所有边的边权最小值最大 如果可以无限大,输出 ...

  4. 线上服务器TCP被打满是啥情况

    从一个线上服务器警告谈谈backlog https://wangxiangnan.cc/?p=105 缘起 双十一如期而至,此时的我因为在处理客户的一个问题已经陷入了忙碌.突然,不断接到驻场实施发来的 ...

  5. 常见web安全攻防总结

    Web 安全的对于 Web 从业人员来说是一个非常重要的课题 , 所以在这里总结一下 Web 相关的安全攻防知识,希望以后不要再踩雷,也希望对看到这篇文章的同学有所帮助.今天这边文章主要的内容就是分析 ...

  6. 利用OCR文字识别+百度算法搜索,玩转冲顶大会、百万英雄、芝士超人等答题赢奖金游戏

    [先上一张效果图]: 一.原理: 其实原理很简单: 1.手机投屏到电脑: 2.截取投屏画面的题目部分,进行识别,得到题目和三个答案: 3.将答案按照一定的算法,进行搜索,得出推荐答案: 4.添加了一些 ...

  7. js基础篇(dom操作,字符串,this等)

    首先我们来看这样一道题 <div id='foo' class='aa bb cc'></div>写出如何判断此div中是否含有aa(注:aa成立,aaa不成立) 首先,我们一 ...

  8. Django 组件-分页器

    Django的分页器(paginator) view from django.shortcuts import render,HttpResponse # Create your views here ...

  9. C++ 数据的封装 初始封装

    C++ 数据封装 所有的 C++ 程序都有以下两个基本要素: 程序语句(代码):这是程序中执行动作的部分,它们被称为函数. 程序数据:数据是程序的信息,会受到程序函数的影响. 封装是面向对象编程中的把 ...

  10. E: Invalid operation build-depgcc(给字符界面的ubuntu安装gcc 报错

    sudo apt-get  build-depgccE: Invalid operation build-depgcc 原因是中间少了个空格, 使用如下命令即可. sudo apt-get  buil ...