[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 ...
随机推荐
- 《高性能SQL调优精要与案例解析》新书样章
该书样章已上传,需要的同学可以通过如下地址下载:http://www.itpub.net/thread-2091839-1-1.html http://www.itpub.net/thread-209 ...
- Android Studio打包生成APK教程
一.修改版本和指定生成APK文件名[可选] 将项目切换到Project视图,打开app目录下的build.gradle文件 1.1 修定软件版本 如1.2图所示. versionCode是app的大版 ...
- qt窗口最小化之后无法打开
转自: https://blog.csdn.net/qiangzi4646/article/details/79743604 http://www.cnblogs.com/lingdhox/p/331 ...
- jQuery封装 写的的确不错 转载
扩展jQuery插件和方法的作用是非常强大的,它可以节省大量开发时间.这篇文章将概述jQuery插件开发的基本知识,最佳做法和常见的陷阱. 入门 编写一个jQuery插件开始于给jQuery.fn加入 ...
- highcharts,highStock 中文图表配置
感谢开源的支持! https://github.com/hcharts/highcharts-zh_CN
- Bootstrap--思维导图
Bootstrap--思维导图
- 打开和写入word文档
一. 使用win32读取word内容 # -*- coding: utf-8 -*- from win32com import client as wc def readDocx2(): word = ...
- DevExpress WinForms使用教程:新的CheckEdit样式
[DevExpress WinForms v18.2下载] 在最开始CheckEdit控件有16种样式, 使用CheckStyle属性,开发人员可以选择其中一种样式.随着时间推移,与其他Windows ...
- <codis><jodis>
Overview For codis and jodis. Codis TBD... Jodis Java client for codis. Jodis is a java client for c ...
- 7-log4j2之自定义Appender
一.添加Maven依赖 <dependencies> <dependency> <groupId>org.apache.logging.log4j</grou ...