Description

Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list should be made by splicing together the nodes of the two lists and sorted in ascending order.

Example

Given 1->3->8->11->15->null2->null , return 1->2->3->8->11->15->null.

解题:很经典的一道题目,有序链表的合并。LintCode中链表默认没有头指针,不过此题可以构造一个带头结点的链表存放结果,最后返回的时候把头结点去掉即可。代码如下:

/**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/ public class Solution {
/**
* @param l1: ListNode l1 is the head of the linked list
* @param l2: ListNode l2 is the head of the linked list
* @return: ListNode head of linked list
*/
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
// write your code here
ListNode nhead = new ListNode(0);
ListNode rear = nhead;
while(l1 != null && l2 != null){
if(l1.val < l2.val){
rear.next = l1;
l1 = l1.next;
}else{
rear.next = l2;
l2 = l2.next;
}
rear = rear.next;
rear.next = null;
}
if(l1 != null){
rear.next = l1;
}else{
rear.next = l2;
}
return nhead.next; //关键
}
}

165. Merge Two Sorted Lists【LintCode by java】的更多相关文章

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

  2. 156. Merge Intervals【LintCode by java】

    Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...

  3. 两个升序链表的合并 Merge Two Sorted Lists 【 leetcode】

    class Solution {public:   ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {   ListNode *p; ListN ...

  4. 177. Convert Sorted Array to Binary Search Tree With Minimal Height【LintCode by java】

    Description Given a sorted (increasing order) array, Convert it to create a binary tree with minimal ...

  5. 30. Insert Interval【LintCode by java】

    Description Given a non-overlapping interval list which is sorted by start point. Insert a new inter ...

  6. 212. Space Replacement【LintCode by java】

    Description Write a method to replace all spaces in a string with %20. The string is given in a char ...

  7. 173. Insertion Sort List【LintCode by java】

    Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...

  8. 172. Remove Element【LintCode by java】

    Description Given an array and a value, remove all occurrences of that value in place and return the ...

  9. 155. Minimum Depth of Binary Tree【LintCode by java】

    Description Given a binary tree, find its minimum depth. The minimum depth is the number of nodes al ...

随机推荐

  1. tomcat端口被占用如何解决

    有时候我们在eclipse中启动项目时,Tomcat服务器会报错,显示8080.8009.8005这几个端口被占用,此时你用debug启动项目时会发现不管用,console控制台什么信息也没有,此时产 ...

  2. 工具 | Axure基础操作 No.3

    下午了,再来补一些学习,今天东西不多哦,感觉慢慢上手了. 1.设置元件禁用状态 2.设置单选按钮唯一选中 注意这里在浏览器中就只能唯一选中了. 3.设置图片上的文字 4.图片的切割和裁剪 5.嵌入多媒 ...

  3. iOS视频处理

    在iOS中,apple提供了AVFoundation 用来处理音视频,基本能满足一些常用的音视频处理需求,而且能调用的硬件编解码接口,能提高不少效率,这是其它库所不能达到的.最近做过的Recnow S ...

  4. H5输入框在输入信息的时候 页面会变形 并且在页面不变形的时候 键盘会遮挡 输入框的解决办法

    $(document).ready(function () { $('body').css({'height':$(window).height()})});//这行是解决输入框在输入信息弹出键盘后页 ...

  5. 如何编写及运行JS

    JS也是一种脚本语言,他可以有两种方式在HTML页面进行引入,一种是外联,一种是内部.       外联JS的写法为: <script src="相对路径"></ ...

  6. c++string标准输入和getline()整行读入

    1.使用标准IO操作读写string对象 我们都知道,使用标准iostream操作来读写int ,double等内置类型的值,同样,我们也可以使用IO操作来读写string对象. c++ code: ...

  7. python函数中闭包的概念说明

    函数中闭包的概念说明 闭包: 内层函数对外层函数非全局变量的引用,就叫做闭包 判断闭包方法 ._closure_ : 执行后返回有效信息就是闭包,返回none就不是闭包 举例1: 是闭包 def wr ...

  8. 个人对js面向对象和封装插件的用法

    做了一段时间的前端了,给自己写代码也总结出来了一点小小的思路,就来分享一下自己的意见和建议了. 面向对象和插件封装其实说到底都是面向对象的思想,但是插件一般是你要用的时候就new 调用出来的.就说一下 ...

  9. PHP环境配置:Windows7+IIS7+PHP+MySQL - 适用于(2008 R2 / 8 / 10)

    配置需求 操作系统:Windows7(x32/x64), windows2008 IIS版本:7.0 PHP版本:7.0.6 及以上 MySQL版本:5.7.12 及以上 第一步:安装 IIS 注意: ...

  10. C语言之二叉树

    规定:根节点的值大于左节点但小于右节点的值,所以二叉树的值插入是唯一的,最后形成的树只跟根节点有关 定义节点: struct tree_node {      TypeElem  elem; stru ...