165. Merge Two Sorted Lists【LintCode by java】
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->null, 2->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】的更多相关文章
- 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 ...
 - 156. Merge Intervals【LintCode by java】
		
Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...
 - 两个升序链表的合并 Merge Two Sorted Lists 【 leetcode】
		
class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode *p; ListN ...
 - 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 ...
 - 30. Insert Interval【LintCode by java】
		
Description Given a non-overlapping interval list which is sorted by start point. Insert a new inter ...
 - 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 ...
 - 173. Insertion Sort List【LintCode by java】
		
Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...
 - 172. Remove Element【LintCode by java】
		
Description Given an array and a value, remove all occurrences of that value in place and return the ...
 - 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 ...
 
随机推荐
- NSMutableArray和NSArray的常用方法及相互转换
			
NSMutableArray和NSArray的常用方法及相互转换 // NSArray --> NSMutableArray NSMutableArray *myMutableArray = [ ...
 - window7及以上 创建软链接 mklink
			
软链接是一种文件共享方式. 命令:mklink /d "C:\d" "C:\e" 有哪些坑: 1.此命名必须以管理员方式在cmd运行 2.文件必须不存在..通过 ...
 - 关于从Oracle数据库中删除表数据
			
1,删除表 drop 1.1 执行drop table table_name 语句 被 drop后的表被放在用户回收站(user_recyclebin)里,而没有被直接删除掉,回收站里的表可以被恢复 ...
 - python    输入三个整数,按照从小到大的顺序打印
			
# # 3 输入三个整数,按照从小到大的顺序打印 a = int(input('请输入第一个整数:')) b = int(input('请输入第二个整数:')) c = int(input('请输入第 ...
 - SpringBoot整合Swagger2以及生产环境的安全问题处理
			
1.创建springboot项目 https://www.cnblogs.com/i-tao/p/8878562.html 这里我们使用多环境配置: application-dev.yml(开发环境) ...
 - fastdfs安装过程
			
Fastdfs于centos7的安装步骤(支持横向拓展) 主要目的:根据网上教程搭建时遇到的问题以及描述不明确的地方进行补充和说明 一.首先需要准备以下4个文件 nginx-1.12.0.tar.gz ...
 - Vue性能优化之组件按需加载(以及一些常见的性能优化方法)
			
关于Vue中的按需加载我就简单介绍一下:大概就是我们所有的东西都会在app.js里面,但是我们并不需要把所有的组件都一次性加载进来,我们可以在需要它的时候再将它加载进来,话不多说,开车! 1.webp ...
 - margin中的bug解决方法
			
margin bug问题 : 当做子元素中使用margin-top: 50px;父子元素都会跑出50px, 解决方法: 在父元素中使用下面三种任意一种都可以. 方法一:给父元素加边框 border: ...
 - 2.2 vivi虚拟视频驱动测试
			
学习目标:在linux终端安装xawtv,并测试vivi.ko驱动程序. 一.安装xawtv 1)ubuntu能上网情况下,使用命令:# sudo apt-get install xawtv 2)如果 ...
 - Python(ATM机low版)
			
import osclass ATM: @staticmethod def regst(): while 1: nm = input('请输入你的名字:') mm = input('请输入你的密码:' ...