题目: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. cmake find_package 中,include_directories,target_link_libraries 的值怎么知道?

    拿Sophus库为例: find_package(Sophus REQUIRED) include_directories(${Sophus_INCLUDE_DIRS}) target_link_li ...

  2. Could not find a package configuration file provided by "Sophus",SophusConfig.cmake

    CMake Error at CMakeLists.txt:5 (find_package): By not providing "FindSophus.cmake" in CMA ...

  3. Linux ssh命令

    SSH(远程连接工具)连接原理:ssh服务是一个守护进程(demon),系统后台监听客户端的连接,ssh服务端的进程名为sshd,负责实时监听客户端的请求(IP 22端口),包括公共秘钥等交换等信息. ...

  4. 43 【redis cluster】

    有两篇文章不错,可以看下: 1,初步理解redis cluster:https://blog.csdn.net/dc_726/article/details/48552531 2,仔细理解redis ...

  5. Activex、OLE、COM、OCX、DLL之间的区别

    先明确组件(Component)和对象(Object)之间的区别: 组件是一个可重用的模块,它是由一组处理过程.数据封装和用户接口组成的业务对象(Rules Object).组件看起来像对象,但不符合 ...

  6. 什么叫做API?看完你就理解了

    阅读编程资料时经常会看到API这个名词,网上各种高大上的解释估计放倒了一批初学者.初学者看到下面这一段话可能就有点头痛了. API(Application Programming Interface, ...

  7. python 2与python3 区别

    源码区别 python3:python2 a) py3 优美简单清晰. b) py2:源码重复,混乱,不规范,冗(rong)余(不需要特多,啰嗦). test a)    py3:可以中文也可以英文( ...

  8. IFrame跨域访问&&IFrame跨域访问自定义高度

    1.IFrame跨域访问: http://blog.csdn.net/fdipzone/article/details/17619673 2.IFrame跨域访问自定义高度: 由于JS禁止跨域访问,如 ...

  9. vue子传父多个值

    子组件:this.$emit("methdosName",data1,data2,data3) 父组件: <child @methodsName="xxx(argu ...

  10. (转)Flex 布局教程:

    这个博客的内容比较新,多看看 http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html [语法篇] http://www.ruanyifeng. ...