leetcode-algorithms-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 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)
{
if (l1 == nullptr && l2 == nullptr) return nullptr; ListNode *head = new ListNode(0);
ListNode *l = head;
while(l1 != nullptr && l2 != nullptr)
{
if (l1->val <= l2->val)
{
l->next = l1;
l1 = l1->next;
}
else
{
l->next = l2;
l2 = l2->next;
}
l = l->next;
}
l->next= l1 ? l1 : l2; return head->next;
}
};

链接: leetcode-algorithms 目录

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

  3. 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...

  4. Leetcode练习题21. Merge Two Sorted Lists

    题目描述(easy) Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new ...

  5. 【一天一道LeetCode】#21. Merge Two Sorted Lists

    一天一道LeetCode系列 (一)题目 Merge two sorted linked lists and return it as a new list. The new list should ...

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

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

  9. LeetCode:21. Merge Two Sorted Lists(Easy)

    1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ...

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

随机推荐

  1. nginx重新编译添加ssl模块

    https://www.cnblogs.com/zhming26/p/6278667.html https nginx配置 找到安装nginx的源码根目录,如果没有的话下载新的源码 http://ng ...

  2. (转) GAN应用情况调研

    本文转自: https://mp.weixin.qq.com/s?__biz=MzA5MDMwMTIyNQ==&mid=2649290778&idx=1&sn=9816b862 ...

  3. dataTable表头未对其解决方法

    本文为博主原创,未经允许不得转载: 在tab页中使用dataTable时,默认显示的dataTable表头与数据显示正常,另一个的datatable则表头与数据未对其. 检查元素发现,datatabl ...

  4. Thymeleaf 模板引擎技术

    引入Thymeleaf: <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf. ...

  5. 项目Alpha冲刺--2/10

    项目Alpha冲刺--2/10 1.团队信息 团队名称:基于云的胜利冲锋队 成员信息 队员学号 队员姓名 个人博客地址 备注 221500201 孙文慈 https://www.cnblogs.com ...

  6. _itemmod_gem_limit

    该表可以控制特定宝石的数量上限,即使玩家多插了宝石,也不会有相应效果 `entry` 宝石ID `limitCount`上限值 `comment`备注

  7. js,jq获取父,兄弟,子节点整理

    js获取节点 父: parentNode 获取已知节点的父节点. 子: childNodes; 得到全部子节点 children 得到全部子节点 firstChild 获得第一个子节点 lastChi ...

  8. ORACLE SYNONYM详解

    以下内容整理自Oracle 官方文档 一 概念 A synonym is an alias for any table, view,materialized view, sequence, proce ...

  9. 一篇很好的java异常框架讲解

    https://www.cnblogs.com/itcui/p/6400499.html 其实原作者是csdn的一名博主,实在受不了csdn的广告,所以就摘录这篇

  10. MYSQL常用函数(时间和日期函数)Java中

    CURDATE()或CURRENT_DATE() 返回当前的日期 CURTIME()或CURRENT_TIME() 返回当前的时间 DATE_ADD(date,INTERVAL int keyword ...