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

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

 /**
* 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. Linux Shell编程(1)——shell编程简介

    Shell是一个命令解释器.它不仅是操作系统内核与用户之间的绝缘层,同时也是一种功能相当强大的编程语言.一个Shell程序,通常称为脚本,它是一个由系统调用,命令工具,软件包和已编译的二进制包&quo ...

  2. HDU 5478 Can you find it(数学问题)

    题目大意: 给你  ak1⋅n+b1+ bk2⋅n−k2+1 = 0 (mod C)(n = 1, 2, 3, ...). 要求所有的n都满足上述的式子. 问这样的a,b 有多少对?   分析这个问题 ...

  3. 【模拟】Codeforces 691A Fashion in Berland

    题目链接: http://codeforces.com/problemset/problem/691/A 题目大意: n个数0或1,要求恰好n-1个1,如果n为1则那个数一定要是1 题目思路: [模拟 ...

  4. RHEL 6.4 安装svn和apache

    1.安装软件包(RHEL已经按默认设置安装完成) 需要安装的.rpm软件包: postgresql-libs perl-URI subversion mod_dav_svn 2.创建svn文件夹和版本 ...

  5. motan源码分析九:开关

    在前面的文章中,我们已经发现了开关的踪影,例如cluster,motan支持多个cluster,当前的cluster因为开关关闭的情况下,就会使用下一个cluster. 1.开关相关的类和接口主要都在 ...

  6. 一个小程序,时间util

    比较时间,如果此时间是今天的显示时间点,今天以前显日期 public String getTime(Date time){               SimpleDateFormat dateFor ...

  7. slidingmenu+fragment实现经常使用的側滑效果(包含Fragment状态的保存)

    一.需求 关于fragment的问题,一直想写一篇博客了.应该当初自己也是对这玩意一点都不熟悉到如今也大概知道个日常的使用的地步. 一个側滑的导航栏,内有4个条目.每个选项点击进入相应的界面,每个界面 ...

  8. POJ 3978 Primes(求范围素数个数)

    POJ 3978 Primes(求范围素数个数) id=3978">http://poj.org/problem? id=3978 题意: 给你一个区间范围A和B,要你求出[A,B]内 ...

  9. @property的特性

    @property还有一些关键字,它们都是有特殊作用的,比如上述代码中的nonatomic,strong: 1 2 @property(nonatomic,strong) NSString *carN ...

  10. URL重写:RewriteCond指令与RewriteRule 指令格式(转)

    Rewirte主要的功能就是实现URL的跳转和隐藏真实地址,基于Perl语言的正则表达式规范.平时帮助我们实现拟静态,拟目录,域名跳转,防止盗链等.本文将针对mod_rewrite和URL匹配的技术细 ...