[LeetCode&Python] Problem 21. Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if not l1 and l2:
return l2
if not l2 and l1:
return l1
if not l1 and not l2:
return [] it1=l1
it2=l2
if it1.val<=it2.val:
node=ListNode(it1.val)
it1=it1.next
else:
node=ListNode(it2.val)
it2=it2.next
ll=node
while it1 and it2:
if it1.val<=it2.val:
node1=ListNode(it1.val)
it1=it1.next
node.next=node1
node=node1
else:
node1=ListNode(it2.val)
node.next=node1
node=node1
it2=it2.next if it1:
node.next=it1
elif it2:
node.next=it2
return ll
[LeetCode&Python] Problem 21. Merge Two Sorted Lists的更多相关文章
- 【leetcode❤python】21. Merge Two Sorted Lists
		
#-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):# def __init ...
 - leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)
		
1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...
 - LeetCode记录之21——Merge Two Sorted Lists
		
算法和数据结构这东西,真的是需要常用常练.这道看似简单的链表合并题,难了我好几个小时,最后还是上网搜索了一种不错算法.后期复习完链表的知识我会将我自己的实现代理贴上. 这个算法巧就巧在用了递归的思想, ...
 - [Leetcode][Python]21: Merge Two Sorted Lists
		
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...
 - 21. Merge Two Sorted Lists(合并2个有序链表)
		
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...
 - 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 ...
 - 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists
		
21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...
 - 刷题21. Merge Two Sorted Lists
		
一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...
 - [LeetCode] 21. 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 ...
 
随机推荐
- jvm加载类(更新中)
			
作为jvm的用户,从使用者角度来看,我们给jvm输入一个class文件,得到了一个Class对象.我们可以猜想下jvm加载类的过程:class文件有规定的格式,jvm去解析class文件流,读magi ...
 - Python3+ssl实现加密通信
			
一.说明 1. python标准库ssl可实现加密通信 2. ssl库底层使用openssl,做了面向对像化改造和简化,但还是可以明显看出openssl的痕迹 3. 本文先给出python实现的soc ...
 - [x] 封装、继承,多态
			
那么多态的作用是什么呢,封装可以使得代码模块化,继承可以扩展已存在的代码,他们的目的都是为了代码重用. 而多态的目的则是为了接口重用.也就是说,不论传递过来的究竟是那个类的对象,函数都能够通过同一个接 ...
 - python 自然语言处理(六)____N-gram标注
			
1.一元标注器(Unigram Tagging) 一元标注器利用一种简单的统计算法,对每个标注符分配最有可能的标记.例如:它将分配标记JJ给词frequent,因为frequent用作形容词更常见.一 ...
 - nginx配置文件详解(三)
			
nginx配置文件详细解析 nginx安装目录: /usr/local/nginx 配置文件: /usr/local/nginx/conf 目录下的 nginx.conf文件 nginx优化方法1 ...
 - xpath  & <tr><td><br>
			
python : 3.6 lxml : 4.2.1 from lxml.html import etree test_html = ''' <!DOCTYPE html PUBLIC " ...
 - [转]如何远程连接运行OpenGL/Cuda 等GPU程序
			
发现一篇神文,解决了困扰许久的远程桌面OpenGL/GPU 等问题... 原地址在这:http://www.tanglei.name/how-to-run-gpu-programs-using-rem ...
 - java8新特性Lambda练习
			
练习内容: 1.实现案例 /** *使用lambda表达式定义Collections.sort() */ /** *使用lambda表达式自定义比对方式 */ @Test public void Te ...
 - 第二章 使用unittest模块扩展功能测试
			
2.1使用功能测试驱动开放一个最简单的应用 # functional_tests.py # -*- coding: utf-8 -*- from selenium import webdriver b ...
 - 玩转X-CTR100 l STM32F4 l 定时器时间测量
			
我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] 本文介绍X-CTR100控制器 使用处理器内部硬件定 ...