描述:

合并两个有序链表。

解决:

ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (!l1)
return l2;
if (!l2)
return l1;
if (!l1 && !l2)
return NULL; ListNode* ret = new ListNode();
ListNode* now = ret;
while (l1 || l2) {
if (!l1) {
now->next = l2;
break;
}
else if (!l2) {
now->next = l1;
break;
} ListNode** min = l1->val <= l2->val?&l1:&l2;
now->next = *min;
auto save = (*min)->next;
(*min)->next = NULL;
*min = save;
now = now->next;
}
return ret->next;
}

leetcode 21 Merge Two Sorted Lists 合并两个有序链表的更多相关文章

  1. LeetCode 21. Merge Two Sorted Lists合并两个有序链表 (C++)

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  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】21. Merge Two Sorted Lists 合并两个有序链表

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

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

    这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...

  5. 21. Merge Two Sorted Lists(合并2个有序链表)

    21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...

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

  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 splicing t ...

  8. 021 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 ...

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

随机推荐

  1. ASP.NET FORM认证配置排错记录

    搞了2小时都不能实现自动跳转到登录页面,后删除了配置文件中的name,就解决问题了. <authorization>      <deny users="?" / ...

  2. Java---SSH(MVC)面试

    Java---SSH(MVC) 1.        谈谈你mvc的理解 MVC是Model—View—Controler的简称.即模型—视图—控制器.MVC是一种设计模式,它强制性的把应用程序的输入. ...

  3. DNS记录类型名单

    原文:http://www.worldlingo.com/ma/enwiki/zh_cn/List_of_DNS_record_types DNS记录类型名单 这 DNS记录类型名单 提供一个方便索引 ...

  4. CF gym101933 K King's Colors——二项式反演

    题目:http://codeforces.com/gym/101933/problem/K 每个点只要和父亲不同色就行.所以 “至多 i 种颜色” 的方案数就是 i * ( i-1 )n-1 . #i ...

  5. Makefile编写 三 伪目标的作用

    本节我们讨论一个Makefile中的一个重要的特殊目标:伪目标. 伪目标是这样一个目标:它不代表一个真正的文件名,在执行make时可以指定这个目标来执行其所在规则定义的命令,有时我们也可以将一个伪目标 ...

  6. erlang单独配置文件

    一种是erl启动的时候加参数 doudizhu.config [ {doudizhu,[ {listen_port, }, {node_caller_prefix,"ruby"}, ...

  7. 单链表LRU

    单链表实现lru 越靠近链表尾部的节点是越早之前访问的 当有一个新的数据被访问时,从链表头开始顺序遍历链表 1.如果此数据之前已经被缓存在链表中 遍历得到这个数据对应的节点,并将其从原来的位置删除,然 ...

  8. LeetCode 177. Nth Highest Salary

    https://leetcode.com/problems/nth-highest-salary/description/ Write a SQL query to get the nth highe ...

  9. win10初期版本administrator的限制

    win10初期版本administrator的限制,很多功能administrator用户无法使用如应用商店等等,在目前的新版本,差不多都可以使用了.

  10. django rest_framework 框架的使用03

    rest_framework的 数据解析器 首先需要知道前端发给后端的数据格式头有哪些: media_type = 'application/json' media_type = 'applicati ...