非常easy问题。

唯一的地方可以具有更具挑战是确保不会引入额外的空间。查找开始值最小的名单列表的新掌门人,头从列表中删除。其他操作应该没有问题。

class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
if(l1 == NULL) return l2;
if(l2 == NULL) return l1;
if(l1->val>l2->val)
swap(l1, l2);
ListNode *head = l1, *pNode = l1;
l1 = l1->next;
head->next = NULL;
while(l1&&l2){
if(l1->val <= l2->val){
pNode->next = l1;
pNode = l1;
l1 = l1->next;
}else{
pNode->next = l2;
pNode = l2;
l2 = l2->next;
}
pNode->next = NULL;
}
if(l1)
pNode->next = l1;
if(l2)
pNode->next = l2;
return head;
}
};

版权声明:本文博客原创文章,博客,未经同意,不得转载。

leetcode先刷_Merge Two Sorted Lists的更多相关文章

  1. Java for LeetCode 023 Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解 ...

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

  3. 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]

    题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

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

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

  5. [Leetcode Week4]Merge Two Sorted Lists

    Merge Two Sorted Lists题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-two-sorted-lists/descrip ...

  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] 23. Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...

  8. 【leetcode】Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  9. 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 list sh ...

随机推荐

  1. __weak如何实现目标值自己主动设置nil的

    在开始评论__weak机制之前,首先,一些床上用品 ARC 实现 苹果公司的官方介绍说,.ARC这是"内存管理由编译器"的,但事实上,只有编译器不能完全胜任,ARC另外还要看OC执 ...

  2. opengl微发展理解

    1.什么是OpenGL? 一种程序,可以与界面和图形硬件交互作用.一个开放的标准 2.软件管道 请看上图 - Apllication层     表示你的程序(调用渲染命令.如opengl API) - ...

  3. 第三篇——第二部分——第三文 配置SQL Server镜像——域环境

    原文:第三篇--第二部分--第三文 配置SQL Server镜像--域环境 原文出处:http://blog.csdn.net/dba_huangzj/article/details/28904503 ...

  4. python 2.x 与3.x的区别

    下载了点python的电子书,有基于3.x的有基于2.x的让我不知道看哪些好,BD一下差别着实很大有木有?print语句变成了函数有木有?之后都得加括号了有木有? 别人整理的成果,我就无耻地搬来借用啦 ...

  5. Windows Phone 8 MD5

    using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Wi ...

  6. android 电平信号状态识别View平局

    1.前言 级信号状态View在今天的Android系统是常见.状态的图标就很的经典,有几种状态,到了快没电的时候有些还会闪烁提示用户充电:还有的就是一些地图App的GPS信号强度的提示.Wifi信号强 ...

  7. php session 读写锁

    php session 读写锁 先看一个样例,功能: 1.点击页面中一个button,ajax运行php,php中用session记录运行到哪一步. 2.使用ajax轮询还有一个php,获取sessi ...

  8. hibernate学习笔记(1)hibernate基本步骤

    hibernate基本步骤 1.创hibernate置对象 Configuration config = newConfiguration(); config.configure("hibe ...

  9. Spring.NET程序

    第一个Spring.NET程序   Spring.NET环境准备 pring.NET 1.3.2下载地址:http://down.51cto.com/data/861700 下载后解压 Spring. ...

  10. 新安装Win10

    随着微软发布Windows 10下载技术预览版.现在,您可以免费下载Windows 10技术预览ISO档,安装和开放经验. Windows 10技术预览提供的第一部英语.中国简体.葡萄牙语,含32位. ...