LeetCode专题-Python实现之第21题:Merge Two Sorted Lists
相关代码已经上传到github:https://github.com/exploitht/leetcode-python
文中代码为了不动官网提供的初始几行代码内容,有一些不规范的地方,比如函数名大小写问题等等;更合理的代码实现参考我的github repo
1、读题
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.
这道题说要合并提供的2个排好序的链接表,要求得到的新链表是2个旧链表的结点拼接得来。换言之该索引而不创建结点对象。
2、解题
有序总比无序要简单很多,既然2个链表都是有序的,那么每次都取2个链表的第一个元素中小的一个然后注意边界判断,就能解决问题了。具体逻辑参考代码及注释,代码如下:
# 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
"""
# 2个有序列表合并成1个列表
# 如果l1为空,返回l2;如果l2为空,返回l1
# 如果l1和l2都不为空,result_head取l1和l2中小的一个,小的一个指向自己的next
# 如果有一个列表发现自己的下一个为空,也就是完成遍历,这时候将另外一个列表的next直接给result
# 有一个链表为空则返回另外一个链表
if l1 is None:
return l2
elif l2 is None:
return l1
# flag和head开始是同一个结点,flag引用会不断后移,head负责保留头结点索引
result_flag = ListNode(0)
result_head = result_flag
while True:
# 进入循环的时候2个链表都不为空
# 保证l1指向的结点值小于l2指向的结点值
(l1, l2) = (l1, l2) if l1.val < l2.val else (l2, l1)
# 因为l1是小值,所以丢给前一个结点的next引用,flag索引后移
result_flag.next = l1
result_flag = result_flag.next
# 因为每次取走的都是较小值开头的列表,所以要只能是这个列表先取完(小值还有不会操作大值)
# 故只需要判断小值开头的l1的next是不是空就行
if l1.next is None:
# l1没有下一个元素了,这时候不管l2还有多少元素,直接丢给结果指针的next就行了
result_flag.next = l2
return result_head.next
else:
l1 = l1.next
LeetCode专题-Python实现之第21题:Merge Two Sorted Lists的更多相关文章
- [LC]21题 Merge Two Sorted Lists (合并两个有序链表)(链表)
①英文题目 Merge two sorted linked lists and return it as a new list. The new list should be made by spli ...
- 【LeetCode算法-21】Merge Two Sorted Lists
LeetCode第21题 Merge two sorted linked lists and return it as a new list. The new list should be made ...
- LeetCode专题-Python实现之第28题: Implement strStr()
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第27题:Remove Element
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第20题:Valid Parentheses
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第9题:Palindrome Number
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第14题:Longest Common Prefix
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第13题:Roman to Integer
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
随机推荐
- C++入门笔记(一)零碎基础知识
零碎基础知识 一.创建和运行程序 1.使用文本编辑器编写程序,保存为文件,该文件就叫源代码. 2.编译源代码:运行一个程序,将源代码翻译为主机使用的内部语言----机器语言.包含了 编译后程序的文件就 ...
- sqlzoo:6
第一個例子列出球員姓氏為'Bender'的入球數據. * 表示列出表格的全部欄位,簡化了寫matchid, teamid, player, gtime語句. 修改此SQL以列出 賽事編號matchid ...
- 用clumsy模拟丢包测试socket库的失败重传
用python的socket库写了通信小程序,现在我需要通过软件模拟出在网络极差的情况下,socket底层解决丢包问题的能力怎么样,我一开始想的是分别在linux和windowns下分别测试,后来一想 ...
- 数据分析——pyecharts
导入类库 from pyecharts import Pie, Bar, Gauge, EffectScatter, WordCloud, Map, Grid, Line, Timeline impo ...
- 详解Session和cookie
1.cookie 1.1. 为什么会有cookie? 由于HTTP是无状态的,服务端并不记得你之前的状态.这种设计是为了HTTP协议的方便,但是也存在一些问题.比如我们登录一个购物网站,我们需要用户登 ...
- centos7.5安装python3.7
系统状态 CentOS Linux release 7.5.1804 (Core) mini版安装系统 Python-3.7.0.tgz 官方下载源码包 安装系统依赖包 # 编译必备 yum inst ...
- RabbitMQRPC 官方demo
public class RPCServer { public static void Test() { var factory = new ConnectionFactory() { HostNam ...
- c# 集合的长度为什么是可变的
摘要: 写在前面:此随笔仅仅是作为个人学习总结,有不对的地方,请各位前辈指正O(∩_∩)O........ 一: 引入 在学习集合之前我们都学习过数组.可以知道数组的长度在声明的时候就已经被固定了,不 ...
- sql server创建登录出发器后导致登录失败--解决方案
1.选择sql server配置管理器---sql server服务--右键属性--启动参数--添加-f.-m两个参数并重启sql server服务 2.重新启动sql server以windos身份 ...
- 出现 HTTP Status 500 - Servlet.init() for servlet springmvc threw exception 异常的原因及解决方法
今天做项目的时候遇到了这个问题 其中有一句是Caused by: org.springframework.beans.factory.BeanCreationException: Error crea ...