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 文中代码为了不动官网提供的初始 ...
随机推荐
- Android中Adapter类的使用 “Adapter”
Adapter用来把数据绑定到扩展了AdapterView类的视图组(例如:ListView或Gallery).Adapter负责创建代表所绑定父视图中的底层数据的子视图. 可以创建自己的Adapte ...
- sqlzoo:6
第一個例子列出球員姓氏為'Bender'的入球數據. * 表示列出表格的全部欄位,簡化了寫matchid, teamid, player, gtime語句. 修改此SQL以列出 賽事編號matchid ...
- Egret获取和显示时间,年,月,日,时分秒
let now = new Date(); this.nowYear = now.getFullYear(); this.nowMonth = now.getMonth() + 1; let noww ...
- go-mod 入门
Q群有人问go mod 问题,自己也忘了些.顺便再整理下. GO111MODULE可以设置为三个字符串值之一:off,on或auto(默认值). off 则go命令从不使用新模块支持.它查找vendo ...
- linux crontab 执行mysqldump全局备份为空
今天遇到个问题,在定时备份时 去查看备份文件,发现大小竟然为0,执行 备份sh文件备份, 备份的sql文件大小正常.试了几种办法. 最终解决办法: 问题原因: 因为我设置的环境变量 就直接在sh中 使 ...
- Centos服务器上NFS灾备环境及KVM的搭建及使用
1.概述 由于在单台服务器上搭建灾备环境需要KVM和NFS的支持,下面先列出KVM的搭建流程,再列出使用NFS实现单台服务器灾备的流程. A.搭建KVM环境 1>.主机环境准备 Linux Sy ...
- C# 对串口的操作
初始化 串口 SerialPort sp = new SerialPort(); sp.PortName = BasicParameters.IniReadValue(strPath, "C ...
- 样式布局与 BFC
一.几类视图 内联视图:inline 单行 块级视图:block 换行,有高度 行内块级视图:inline-block 单行,有高度 二.几类布局 块级布局 换行,通过设置 margin 水平居中 & ...
- JS 多选文件或者选择文件夹
<%--文件多选--%> <input type="file" name="file" id="file" multipl ...
- 初识CUDA
如果问题规模较小,逻辑控制较为复杂,并行性很小优先使用CPU处理该问题,如果包含较大规模的数据处理,则考虑使用GPU进行处理. CPU上线程是重量级实体,可以开启1~32个线程,且上下文切换较为缓慢, ...