[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 together the nodes of the first two lists.
题意:合并两个排好的链表并返回新的链表。
可以使用归并排序,从两链表的表头,取出结点,比较两值,将较小的放在新链表中。如1->3->5->6和2->4->7->8,先将1放入新链表,然后将3和2比较,较小值放入新链表中,从1到3只要pre=pre->next即可。当其中有一条链表被访问完,结束循环,找出该链表,将其接在新链表的后面即可。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2)
{
ListNode *newList=new ListNode(-);
ListNode *pre=newList;
while(l1&&l2)
{
if(l1->val > l2->val)
{
pre->next=l2;
l2=l2->next;
}
else
{
pre->next=l1;
l1=l1->next;
}
pre=pre->next;
}
if(l1)
pre->next=l1;
else
pre->next=l2; return newList->next;
}
};
[Leetcode] Merge two sorted lists 合并两已排序的链表的更多相关文章
- 【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 ...
- 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- [Leetcode] Merge k sorted lists 合并k个已排序的链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思 ...
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- 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 ...
- [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 ...
- 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 ...
- [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 ...
- leetcode 21 Merge Two Sorted Lists 合并两个有序链表
描述: 合并两个有序链表. 解决: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) ...
随机推荐
- Trie(字典树,前缀树)_模板
Trie Trie,又经常叫前缀树,字典树等等. Trie,又称前缀树或字典树,用于保存关联数组,其中的键通常是字符串.与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中的位置决定.一个节点的 ...
- python之三元运算
三元运算(三目运算):用于较简单的判断(if else). if True: return s = "aaaa" else: return s = "bbbb&quo ...
- python函数(2017-8-2)
1. def 函数名(形式参数) 函数体 return "123" 函数执行了return之后就不再执行下面的代码 2. 默认形参实参的位置一一对应 如果要调整位置,指定形参名字 ...
- [转载]三小时学会Kubernetes:容器编排详细指南
原翻译by梁晓勇 原英文:Learn Kubernetes in Under 3 Hours: A Detailed Guide to Orchestrating Containers 我很奇怪,为什 ...
- Educational Codeforces Round 47 (Rated for Div. 2) :A. Game Shopping
题目链接:http://codeforces.com/contest/1009/problem/A 解题心得: 题意就是给你两个数列c,a,你需要从c中选择一个子串从a头开始匹配,要求子串中的连续的前 ...
- scrapy编写爬虫的时候出现缺少win32api
环境:python3.6 工具:pycharm2017.3 scrapy fetch http://www.baidu.com ModuleNotFoundError: No module named ...
- Java-Swing中使用Web富文本编辑器
资料下载 (截取出了邮件发送的功能.) 2018/11/10 因为要 win7 电脑 IE 8 的原因,使用了 jxBrower 拓展,更容易使用,参考链接(推荐) 问题介绍 window客户端软件的 ...
- C# 控制台应用程序输出颜色字体
最佳解决方案的代码: static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Green; Console.W ...
- 两种方法实现Python二分查找算法
两种方法实现Python二分查找算法 一. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 arr=[1,3,6,9,10,20,30] def findnumber( ...
- Vue-router用法
#全局守卫- router.beforeEach(to,from,next){} #全局后置钩子- router.afterEach(to,from){} #路由独享守卫- beforeEnter(t ...