题目意思:对两个递增链表进行归并排序

思路:没什么好说的,二路归并

 /**
* 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* head=new ListNode();
ListNode* p=head;
while(l1&&l2){
if(l1->val<=l2->val){
p->next=l1;
l1=l1->next;
p=p->next;
}
else{
p->next=l2;
l2=l2->next;
p=p->next;
}
}
if(l1)p->next=l1;
if(l2)p->next=l2;
return head->next; //第一个节点去掉
}
};

21 Merge Two Sorted Lists(两链表归并排序Easy)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. [Leetcode][Python]21: Merge Two Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...

  4. 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists

    21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...

  5. 21. Merge Two Sorted Lists【easy】

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

  6. leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)

    1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...

  7. 刷题21. Merge Two Sorted Lists

    一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...

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

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

  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. 【转】Android JNI编程—JNI基础

    原文网址:http://www.jianshu.com/p/aba734d5b5cd 最近看到了很多关于热补的开源项目——Depoxed(阿里).AnFix(阿里).DynamicAPK(携程)等,它 ...

  2. 线程调用UpdateData函数出错

    在尝试线程更新界面时,在线程中调用UpdateData(FALSE)后出现如下错误: 原因: MFC对象不支持多线程操作,不能供多个线程进程使用.子线程调用pDlg-> UpdateData(F ...

  3. 快速傅里叶变换(FFT):COGS 2216. 你猜是不是KMP

    2216. 你猜是不是KMP ★★★☆   输入文件:guess.in   输出文件:guess.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] XX在玩两个串的游戏. ...

  4. 数据结构(主席树):COGS 2213. K个串

    2213. K个串 ★★★★   输入文件:bzoj_4504.in   输出文件:bzoj_4504.out   简单对比时间限制:20 s   内存限制:512 MB [题目描述] 兔子们在玩k个 ...

  5. 【贪心】HDU 5783 Divide the Sequence

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5783 题目大意: 把一个N个数的数列拆成若干段,保证每一段的前缀和都非负,求最多能拆成多少段. 题目 ...

  6. getting start with storm 翻译 第八章 part-2

    转载请注明出处:http://blog.csdn.net/lonelytrooper/article/details/12435641 The Bolts 首先我们看一下该topology中的标准bo ...

  7. scipy安装失败

    pip install scipy安装失败 可以从uci网站下载wheel安装包然后执行pip install xx.whl进行安装 http://www.lfd.uci.edu/~gohlke/py ...

  8. View获取焦点

    <EditText android:id="@+id/et_phoneNum" android:layout_width="match_parent" a ...

  9. js实现键盘操作对div的移动或改变-------Day43

    <爸爸去哪儿>的第二季据说要开播了额,有点小期待,不知道这一季的小宝贝们会有多萌,还会甜到心底吧, 哈哈,还记得那个风一样的女子呢,不知道她如今怎样了. 言归正传,继续今天的记录,实际上在 ...

  10. [Angualr 2] Watch for changes

    You can watch for form / control changes by using .valueChanges.observe({...}): this.sku.valueChange ...