题目: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. Scrollview包裹布局问题。

    输入框获取焦点,键盘弹出,背景图片上移: https://blog.csdn.net/wljian1/article/details/79962802 android:scrollbarThumbVe ...

  2. Ubuntu 装nexus

    装nexus前提是装好JDK和maven 先下载 wget http://download.sonatype.com/nexus/oss/nexus-2.12.0-01-bundle.tar.gz 再 ...

  3. TaskScheduler

    一初始化 在SparkContext初始化的时候,同时初始化三个对象.DAGScheduler,TaskScheduler,SchedulerBackend.DAGScheduler,前面已经讲到,做 ...

  4. VMware12上安装CentOS7无法上网问题

    常安装使用VMware的搭建集群环境,VMare安装后虚拟机默认的是自动获取IP,有时候用的过程中突然XSHELL中断或者需要固定IP上网,遇到几次居然,但忘了步骤,总结一下,省的每次去找资料 环境配 ...

  5. 解决find命令报错: paths must precede expression

    eg: find . -name *.c -or -name *.cpp 需要将模糊搜索词用引号括起来: find . -name "*.c" -or -name "*. ...

  6. [Java学习]多线程

    关于多进程与多线程 使用多进程的目的:提高CPU利用率. 使用多线程的目的:提高应用程序?利用率. 多线程与多进程区别:进程间内存独立:同一个进程的线程间共享"堆内存和方法区内存" ...

  7. 手动添加jar包到本地maven仓库

    我们都知道使用maven管理jar包的时候,我们需要到远程仓库下载相关的jar包到本地仓库,但是如果远程仓库没有这个jar包呢?这时候我们就需要手动将jar包添加到本地仓库. 起因是我想用百度的富文本 ...

  8. 152. Maximum Product Subarray最大乘积子数组/是否连续

    [抄题]: Given an integer array nums, find the contiguous subarray within an array (containing at least ...

  9. 245. Shortest Word Distance III 单词可以重复的最短单词距离

    [抄题]: Given a list of words and two words word1 and word2, return the shortest distance between thes ...

  10. linux主机名设置

    有时会报错: 代理抛出异常错误: java.net.MalformedURLException: Local host name unknown: java.net.UnknownHostExcept ...