题目:合并两个有序单链表

思路:一开始想复杂了,以为必须在原链表上修改(绕来绕去还AC了,但是思路相当绕),其实没有,按照正常地合并两个数组同样的方法也对。

代码:

public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 == null) return l2;
else if(l2 == null) return l1;
else{
ListNode p = new ListNode(0);
ListNode head = p;
while(l1 != null && l2 != null){
if(l1.val <= l2.val){
p.next = l1;
l1 = l1.next;
}else{
p.next = l2;
l2 = l2.next;
}
p = p.next;
}
while(l1 != null){
p.next = l1;
l1 = l1.next;
p = p.next;
}
while(l2 != null){
p.next = l2;
l2 = l2.next;
p = p.next;
}
return head.next;
}
}

申请一个新链表头head,逐个将较小的元素链到这个链表头的后面,最后返回head.next就可以啦。

[leetcode]_Merge Two Sorted Lists的更多相关文章

  1. [LeetCode] Merge k Sorted Lists 合并k个有序链表

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

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

  3. LeetCode: Merge Two Sorted Lists 解题报告

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

  4. LeetCode: Merge k Sorted Lists 解题报告

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  5. [Leetcode] Merge k sorted lists 合并k个已排序的链表

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

  6. leetcode先刷_Merge Two Sorted Lists

    非常easy问题. 唯一的地方可以具有更具挑战是确保不会引入额外的空间.查找开始值最小的名单列表的新掌门人,头从列表中删除.其他操作应该没有问题. class Solution { public: L ...

  7. LeetCode:Merge k Sorted Lists

    题目链接 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexi ...

  8. LeetCode——Merge k Sorted Lists

    Discription: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its ...

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

随机推荐

  1. [ActionScript 3.0] AS3 3D双圆环贴图

    package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.MovieCl ...

  2. Go Mobile 例子 audio 源码分析

    看这个源码分析前,建议先看更简单地例子 basic 的源码分析(http://www.cnblogs.com/ghj1976/p/5183199.html), 一些基础知识本篇将不再提及. audio ...

  3. Spring配置项<context:annotation-config/>说明

    配置applicationContext.xml时经常会看到: <context:annotation-config/> 它的作用是隐式地向Spring容器注册AutowiredAnnot ...

  4. FreeMarker页面中获得contextPath

    要在ftl页面中使用contextPath,需要在viewResolver中做如下配置(红色部分): <bean id="viewResolver" class=" ...

  5. [ZOJ 1003] Crashing Balloon (dfs搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3 题目大意:给你a,b两个数,问当b由约数1到100组成时,a能否由其 ...

  6. java枚举使用

    1.开发中如何使用枚举,一般在开发中使用消息提示.枚举可以继承,实现接口等.public enum Result { SUCCESS(1,"201 ok") { @Override ...

  7. Centos安装软件小结-20160325

    三种安装包 bin包 rpm包 源码包 1.bin包 1.先赋予权限: chmod 777 *.bin 2.开始安装: ./.bin 2.rpm包(以jdk为例)\ yum search jdk\ y ...

  8. X230上安装Yosemite/Win7-黑苹果之路

    以前曾经在X230上安装了mavericks,但因为无线网卡问题最终作罢,现在换了SSD(128G).AR9285网卡,又冲刺了一把OSX,折腾了好几天,终于成了.特做记录如下: 首先,硬盘分区问题, ...

  9. fw:学好Python必读的几篇文章

    学好Python必读的几篇文章 from:http://blog.csdn.net/hzxhan/article/details/8555602 分类: python2013-01-30 11:52  ...

  10. Windows 2008下部署Exchange Server 2007

    对于很多政府及企业来说,微软的邮件服务器Exchange Server都是一个不错的通信和协作平台选择,尤其新版邮件服务器Exchange Server 2007 和OCS的组合,在微软UC平台上创下 ...