lintcode:合并两个排序链表
题目:
将两个排序链表合并为一个新的排序链表
样例
给出 1->3->8->11->15->null,2->null,
返回 1->2->3->8->11->15->null。
解题:
数据结构中的书上说过,可解,异步的方式移动两个链表的指针,时间复杂度O(n+m)
Java程序:
/**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/
public class Solution {
/**
* @param ListNode l1 is the head of the linked list
* @param 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
if(l1==null && l2!=null)
return l2;
if(l1!=null && l2==null)
return l1;
if(l1==null && l2==null)
return null;
ListNode head = new ListNode(0);
ListNode current = head; while(l1!=null && l2!=null){
if(l1.val<=l2.val){
current.next = l1;
current = current.next;
l1 = l1.next;
}else{
current.next = l2;
current = current.next;
l2 = l2.next;
}
} if(l1!=null)
current.next= l1;
if(l2!=null)
current.next=l2;
return head.next;
}
}
总耗时: 13348 ms
Python程序:
"""
Definition of ListNode
class ListNode(object):
def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param two ListNodes
@return a ListNode
"""
def mergeTwoLists(self, l1, l2):
# write your code here
if l1==None:
return l2
if l2==None:
return l1
if l1==None and l2==None:
return None
head = ListNode(0)
p = head
while l1!=None and l2!=None:
if l1!=None and l2!=None:
if l1.val<= l2.val:
p.next = l1
p = p.next
l1 = l1.next
else:
p.next = l2
p = p.next
l2 = l2.next
if l1==None:
p.next = l2
break
if l2==None:
p.next = l1
break
return head.next
总耗时: 2032 ms
参考剑指OfferP117
利用递归的思想
小的节点链接,下一次递归
递归的好处是不用单独搞个节点当作头节点了,通俗点说是许多头节点连接起来的,最终我们返回的是第一个头节点
/**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/
public class Solution {
/**
* @param ListNode l1 is the head of the linked list
* @param 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
if(l1==null && l2!=null)
return l2;
if(l1!=null && l2==null)
return l1;
if(l1==null && l2==null)
return null;
ListNode MergeHead = null;
if(l1.val < l2.val){
MergeHead = l1;
MergeHead.next = mergeTwoLists(l1.next,l2);
}else{
MergeHead = l2;
MergeHead.next = mergeTwoLists(l1,l2.next);
}
return MergeHead; }
}
Java Code
总耗时: 14047 ms
"""
Definition of ListNode
class ListNode(object):
def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param two ListNodes
@return a ListNode
"""
def mergeTwoLists(self, l1, l2):
# write your code here
if l1==None:
return l2
if l2==None:
return l1
if l1==None and l2==None:
return None
head = None
if l1.val< l2.val:
head = l1
head.next = self.mergeTwoLists(l1.next,l2)
else:
head = l2
head.next = self.mergeTwoLists(l1,l2.next)
return head
Python Code
总耗时: 2403 ms
lintcode:合并两个排序链表的更多相关文章
- LintCode-165.合并两个排序链表
合并两个排序链表 将两个排序链表合并为一个新的排序链表 样例 给出 1->3->8->11->15->null,2->null, 返回 1->2->3- ...
- LintCode 合并两个排序
将两个排序链表合并为一个新的排序链表 样例 给出 1->3->8->11->15->null,2->null, 返回1->2->3->8-> ...
- [剑指offer] 14. 链表中倒数第K个节点+翻转+逆序打印+合并两个排序链表 + 链表相交(第一个公共节点) (链表)
题目描述 输入一个链表,输出该链表中倒数第k个结点. 思路: 两个指针,起始位置都是从链表头开始,第一个比第二个先走K个节点,当第一个走到链表尾时,第二个指针的位置就是倒数第k个节点.(两指针始终相 ...
- LintCode-165 · 合并两个排序链表-题解
描述:将两个排序(升序)链表合并为一个新的升序排序链表样例 1:输入: list1 = null, list2 = 0->3->3->null输出: 0->3->3-&g ...
- 剑指offer—第三章高质量代码(合并两个排序链表)
题目:输入员两个递增排序的链表,合并这两个链表并使新的链表中的结点仍然是按照递增排序的. 思路:首先,定义两个头节点分别为Head1和Head2的链表,然后比较第一个节点的值,如果是Head1-> ...
- 《剑指offer》面试题17—合并两个排序链表
题目:输入两个递增排顺序的链表,合并这两个链表并使合并后的链表仍是递增排序的. 重点: 1.代码鲁棒性:要判断输入的两个链表都为NULL:其中一个链表为NULL的情况. 2.用递归实现,注意递归的思路 ...
- 合并两个排序链表——牛客offer
题目描述: 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 解题思路: 1.一般看到合并这类的题目就会很自然的想到创建一个新的链表,然后将两个链表根据一定 ...
- 【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 ...
- [剑指Offer] 16.合并两个排序链表
题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. [思路1]递归 /* struct ListNode { int val; struct Lis ...
随机推荐
- php关联不上mysql解决办法
## php无法关联mysql php关联不上mysql解决办法: 尝试N多方法,需要的dll文件都复制了,依旧是不断提示: Fatal error: Call to undefined fun ...
- Winform 拦截最小化、最大化、关闭事件【整理】
const int WM_SYSCOMMAND = 0x112; //窗体关闭消息 const int SC_CLOSE = 0xf060; //窗体最小化消息 const int SC_MINIMI ...
- Java基础(二)
下面来实现一个小程序,要求如下: 从键盘接收一个字符串,程序对其中所有的字符进行排序,例如键盘输入:helloitcast程序打印acehillostt 步骤分析: 1.键盘录入字符串,Scanner ...
- 分享:linux下apache服务器的配置和管理
linux下apache服务器的配置和管理. 一.两个重要目录: Apache有两个重要的目录:1.配置目录/etc/httpd/conf:2.文档目录/var/www: 二.两种配置模式: Apac ...
- crontab的应用
当我们需要定时执行某个系统内的php脚本程序时,可以这样设置crontab * 19 * * * /usr/local/php/bin/php /var/www/test.php 此处表示调用php( ...
- php 彩票类 lottery
<?php /* * For the full copyright and license information, please view the LICENSE * file that wa ...
- ASP.NET MVC +EasyUI 权限设计(一)开篇
在前一段时间中,老魏的确非常的忙碌,Blog基本上没有更新了,非常的抱歉,那么在后面的时间中,老魏会尽量的抽时间来写的,可能时间上就不太富裕了.今天开始呢,老魏会和大家分享一下关于权限设计的有关文章, ...
- 面试问到:JDBC、hibernate、ibati
一.JDBC.Connection(连接) 优点:运行高效.快捷. 缺点:代码多.异常多.不支持跨平台. 二.ibatis 1.根据jdbc的基本建立连接. 2.通过anntation+xml.jav ...
- Zabbix实现告警分级
Zabbix中trigger的severity的值定义了trigger的不同严重程度,其中severity默认的6个值为 Not classified, Information, Warning, A ...
- oracle字符集问题总结
在进行web开发和oracle安装的过程中经常有人对字符集搞不清楚,因此对此做一下总结. 1.第一个问题:字符集之间的区别是什么呢? 常见的字符集有:UTF-8和GBK (1)GBK字符集 G ...