题目: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.

简单题,只要对两个链表中的元素进行比较,然后移动即可,只要对链表的增删操作熟悉,几分钟就可以写出来,代码如下:

 struct ListNode {
int val;
ListNode *next;
ListNode(int x):val(x), next(NULL) {}
}; ListNode *GetLists(int n) //得到一个列表
{
ListNode *l = new ListNode();
ListNode *pre = l;
int val;
for (int i = ; i < n; i ++) {
cin >> val;
ListNode *newNode = new ListNode(val);
pre->next = newNode;
pre = pre->next;
}
return l->next;
} ListNode *mergeTwoLists(ListNode *l1, ListNode *l2)
{
assert (NULL != l1 && NULL != l2);
if (NULL == l1 && NULL == l2)
return NULL;
if (NULL == l1 && NULL != l2) // !!要记得处理一个为空,另一个不为空的情况
return l2;
if (NULL != l1 && NULL == l2)
return l1; ListNode *temp = new ListNode();
temp->next = l1;
ListNode *pre = temp; while(NULL != l1 && NULL != l2) {
if (l1->val > l2->val) { //从小到大排列
ListNode *next = l2->next;
l2->next = pre->next;
pre->next = l2;
l2 = next;
}
else {
l1 = l1->next;
}
pre = pre->next;
}
if (NULL != l2) {
pre->next = l2;
}
return temp->next;
}

这其中要注意一点,即要记得处理一个链表为空,另一个不为空的情况,如{}, {0} -- > {0},当然上面的写法多少啰嗦了一些,可以简写。

LeetCode:21_Merge Two Sorted Lists | 合并两个排序列表 | Easy的更多相关文章

  1. [Leetcode] 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. Leetcode21--->Merge Two Sorted Lists(合并两个排序的单链表)

    题目: 给出两个排序的单链表,合并两个单链表,返回合并后的结果: 解题思路: 解法还是很简单的,但是需要注意以下几点: 1.  如果两个链表都空,则返回null; 2.  如果链表1空,则返回链表2的 ...

  3. 【LeetCode每天一题】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 ...

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

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

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

  6. leetcode 21 Merge Two Sorted Lists 合并两个有序链表

    描述: 合并两个有序链表. 解决: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) ...

  7. [LeetCode] Merge k Sorted Lists 合并k个有序链表

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

  8. [Leetcode] Merge k sorted lists 合并k个已排序的链表

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

  9. Leetcode23--->Merge K sorted Lists(合并k个排序的单链表)

    题目: 合并k个排序将k个已排序的链表合并为一个排好序的链表,并分析其时间复杂度 . 解题思路: 类似于归并排序的思想,lists中存放的是多个单链表,将lists的头和尾两个链表合并,放在头,头向后 ...

随机推荐

  1. docker镜像加速器

    目前国内比较靠谱的镜像加速器网址:https://www.daocloud.io/mirror

  2. 安全运维之:Linux后门入侵检测工具的使用

    安全运维之:Linux后门入侵检测工具的使用 https://blog.csdn.net/exitgogo/article/details/39547113

  3. selenium-java,selenium安装配置

    准备材料 1.java jdk http://www.oracle.com/technetwork/java/javase/downloads/index.html 2.开发工具 https://ww ...

  4. c++中被忽视的隐藏

    稍微懂得点oop的人都知道重载,那是多态性的重要体现!可是在c++中你能分清成员函数的重载.覆盖吗?这个好像也不难,重载存在与同一个类中,而覆盖存在于派生类于基类中!可是如果再加上隐藏呢?说实话,以前 ...

  5. swift hidesBottomBarWhenPushed 设置界面

    方法一(推荐):一级界面push的时候设置,子页面无需设置 let vc = JYMyCommissionController() vc.hidesBottomBarWhenPushed = true ...

  6. [leetcode]297. Serialize and Deserialize Binary Tree 序列化与反序列化二叉树

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  7. kafka可视化客户端工具(Kafka Tool)的基本使用

    1.下载 下载地址:http://www.kafkatool.com/download.html 2.安装 根据不同的系统下载对应的版本,我这里kafka版本是1.1.0,下载kafka tool 2 ...

  8. pytbull 手册

    - Official documentation for pytbull v2.1 - Table of content Description Architecture Remote mode Lo ...

  9. ORM常用字段介绍

    Django中的ORM Django项目使用MySQL数据库 1. 在Django项目的settings.py文件中,配置数据库连接信息: DATABASES = { "default&qu ...

  10. Docker Hello World

    Docker 允许你在容器内运行应用程序,使用docker run命令来在容器内运行一个个应用程序. 输出Hello World docker run ubuntu:15.10 ./bin/echo ...