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 ...
随机推荐
- Gradle Goodness: Check Task Dependencies With a Dry Run
We can run a Gradle build without any of the task actions being executed. This is a so-called dry ru ...
- chromium之message_pump_win之三
上一篇分析MessagePumpForUI,参考chromium之message_pump_win之二 MessagePumpForIO,同MessagePumpForUI,也是要实现三个函数 // ...
- MAMP:MySQL wasn't able to start
MAMP 我点击start server的时候 发现mysql服务器打不开 http://images.cnblogs.com/cnblogs_com/lwwen/1231721/o_11111111 ...
- Java中Date类型如何向前向后滚动时间,( 附工具类)
Java中的Date类型向前向后滚动时间(附工具类) 废话不多说,先看工具类: import java.text.SimpleDateFormat; import java.util.Calendar ...
- itertools.groupby()分组字典列表
## itertools.groupby()分组字典列表数据 from operator import itemgetter from itertools import groupby student ...
- MYSQL小函数大用途之-------FIND_IN_SET
没有前言和解释,直接看怎么用 当前我所知道两种用法: 第一种:和like的作用有点相似,但用这个函数会比like更准确的查到你想要的数据. 前提是当前的字段满足俩个要求: 类型为字符型. 储存格式为- ...
- java集合基础篇 简单总结
昨天晚上看了编程思想4的持有对象,所以把学到看到的一些记记背背的方面给总结一下. java的容器主要分为两类,一个是实现了接口Collection的一类,还有一个是实现了Map接口的一类. 继承Col ...
- vmware 虚拟机设置 redhat 桥接模式
1.设置 vmware 网络模式 2.设置 linux 网络模式
- django路由基本使用-6
路由定义位置 django的路由是定义在 urls.py 文件下的 urlpatterns 列表中的. urls.py 文件是路由解析的入口. from django.conf.urls import ...
- python在lxml中使用XPath语法进行#数据解析
在lxml中使用XPath语法: 获取所有li标签: from lxml import etree html = etree.parse('hello.html') print type(html) ...