题目:

  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.

思路:比较每个列表的第一个元素。 合并小的添加到列表中。 最后,当其中一个是空的,只需将它附加到合并后的列表,因为它已经排序。

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head=new ListNode(-1);
ListNode addNode=head;
while(l1!=null&&l2!=null){
if(l1.val<l2.val){
addNode.next=l1;
l1=l1.next;
}else{
addNode.next=l2;
l2=l2.next;
}
addNode=addNode.next;
}
if(l1==null) addNode.next=l2;
if(l2==null) addNode.next=l1;
return head.next;
}
}

【LeetCode】21. Merge Two Sorted Lists的更多相关文章

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

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

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

  3. 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...

  4. 【一天一道LeetCode】#21. Merge Two Sorted Lists

    一天一道LeetCode系列 (一)题目 Merge two sorted linked lists and return it as a new list. The new list should ...

  5. 【LeetCode】023. Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题 ...

  6. 【LeetCode】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 ...

  7. 【LeetCode】23. Merge k Sorted Lists

    合并k个已合并链表. 思路:先把链表两两合并,直到合并至只有一个链表 /** * Definition for singly-linked list. * struct ListNode { * in ...

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

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

  9. C# 写 LeetCode easy #21 Merge Two Sorted Lists

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

随机推荐

  1. 【虚拟化】支持IDE/SATA/SCSI

    驱动说明 IDE    :IDE和SATA是最常用的两种磁盘格式,基本每个系统上都会有: SATA:同IDE: SCSI :系统上一般缺少此驱动,需要手动安装: Linux下的名称 IDE硬盘:hd[ ...

  2. 【Flex学习】Flex4学习网站

    http://blog.minidx.com/category/flex  来自为知笔记(Wiz)

  3. 【Java编程进阶-1】enum枚举的使用

    枚举主要用于枚举常量,下面举个简单的应用. 比如一个公司有如下几个部门: 研发部: 销售部: 财务部: (其他部门暂时不列举) 部门的某些信息相对固定,此时可以考虑使用枚举来说明: 枚举类 Depts ...

  4. HDU3507 print artical

    题目大意:有N个数字a[N],每输出连续的一串,它的费用是 “这行数字的平方加上一个常数M”.问如何输出使得总费用最小.(n<=500000) 分析:动态规划方程为:dp[i]=dp[j]+M+ ...

  5. mysql 常用操作(整理)

    原文地址:http://blog.csdn.net/lxh090821/article/details/9410943 1       登录数据库 格式: mysql -h主机地址 -u用户名 -p用 ...

  6. Linux启动过程详解(转)

    启动第一步--加载BIOS当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它.这是因为BIOS中包含了CPU的相关信息.设备启动顺序信息.硬盘 ...

  7. 在DataList、Repeater的HeaderTemplate和FooterTemplate模板中寻找控件FindControl

    [程序代码] <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> ...

  8. smartgit document merge

    'Normal' Merge In case of a normal merge, a merge commit with at least two parent commits (i.e., the ...

  9. Form_Form标准控件Folder开发解析(案列)

    2014-01-09 Created By BaoXinjian 1. 打开APPSTAND.fmb, 并加载程序库APPFLDR.pll. 2. 基于APPSTAND.fmb生成Folder开发所需 ...

  10. Jmeter监控服务器性能

    JMeter是一款压力测试工具,我们也可以用它来监控服务器资源使用情况.JMeter正常自带可以通过Tomcat的/manager/status来监控服务资源使用情况.这种情况只能监控Tomcat支持 ...