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 ...
随机推荐
- The Linux Kernel
- 安全过滤javascript,html,防止跨脚本攻击
本文改自: http://blog.51yip.com/php/1031.html 用户输入的东西是不可信认的,例如,用户注册,用户评论等,这样的数据,你不光要做好防sql的注入,还要防止JS的注入, ...
- JAVA中时间格式转换
1.将任意日期格式的字符串转换为指定格式的字符串 //默认格式 String s1 = "20190110133236"; //给定格式 String s2 = "201 ...
- OC之block 和协议
一.BOLCK (一)简介 BLOCK是什么?苹果推荐的类型,效率高,在运行中保存代码.用来封装和保存代码,有点像函数,BLOCK可以在任何时候执行. BOLCK和函数的相似性:(1)可以保存代码(2 ...
- LeetCode 简单 -二进制求和(105)
给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11", b = "1" 输出: ...
- 典型的 ajax 异步请求及错误处理
$.ajax({ url: path + '/emergency/saveEmergency.do', async: false,//同步,会阻塞操作 type: 'POST',//PUT DELET ...
- Zookeeper -- 本地\完全分布式 搭建
准备工作 linux软件:Zookeeper-3.4.12.tar.gz 四台centos系统虚拟机,主机名为:s101~s104 一.本地模式搭建(s101上安装) 1.解压软件压缩包:解压到根目录 ...
- 【Android】导航栏(加图片icon)和不同页面的实现(viewpager+tablayout)
先上图,然后说大致步骤,最后再说细节 图片效果:依序点击导航栏左一.左二.中.右二.右一,最后直接滑动页面(不依靠导航栏切换) 大致步骤如下(文末会有完整代码) [1]创建一个类,我这里取名TabBa ...
- UDP server Code
Code Example: The following programs demonstrate the use of getaddrinfo(), gai_strerror(), freeaddri ...
- C语言实验报告(五) 用自定义函数求2~n之间的素数
#include<stdio.h>#include <math.h>int main(){ int i,n; printf("input n:"); ...