导航页-LeetCode专题-Python实现

相关代码已经上传到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的更多相关文章

  1. [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 ...

  2. 【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 ...

  3. LeetCode专题-Python实现之第28题: Implement strStr()

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  4. LeetCode专题-Python实现之第27题:Remove Element

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  5. LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  6. LeetCode专题-Python实现之第20题:Valid Parentheses

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  7. LeetCode专题-Python实现之第9题:Palindrome Number

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  8. LeetCode专题-Python实现之第14题:Longest Common Prefix

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  9. LeetCode专题-Python实现之第13题:Roman to Integer

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

随机推荐

  1. 关于H5在微信浏览器内自动转格式,导致不能正常打开的问题

    从最近开始,微信朋友圈打开我们的H5页面有时会出现了“此网页已被QQ浏览器重新排版”的提示,直接结果就是导致H5网页或H5小游戏不能正常显示了!!这个问题什么严重,不知道腾讯又是怎么考虑的,难道真的是 ...

  2. 虚拟机上的Ubuntu 文件系统成为只读模式的解决办法

    虚拟机环境的Linux系统由于是虚拟化虚拟出来的主机环境,因此 经常会出现一些操作系统的问题,今天我遇到了一个Ubuntu操作系统文件系统成了只读模式,无法进行系统的操作,由于出问题的主机是我个人搭建 ...

  3. SharePoint2016: 使用powerShell启用project web app

    1. 创建pwa承载的webApplication   在SharePoint2016管理中心>应用程序管理>管理web应用程序,新建web应用程序>sharepoint-1001, ...

  4. NPOI操作创建Excel

    一.下载NPOI类库 使用Nuget在线搜索NPOI,下载安装 二.代码开撸 var workBook = new HSSFWorkbook(); #region 设置样式 IFont font = ...

  5. Android第一次作业

    Android第一次作业——天气预报界面 成果图: 思路: 运用RelativeLayout布局管理器来设计整体布局,在其中插入需要的图片和文本框,并设置其字体格式和背景.最后用HorizontalS ...

  6. C# CSV 文件转换成DataTable

    { DataTable dt = new DataTable(); FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess ...

  7. Spring Security中html页面设置hasRole无效的问题

    Spring Security中html页面设置hasRole无效的问题 一.前言 学了几天的spring Security,偶然发现的hasRole和hasAnyAuthority的区别.当然,可能 ...

  8. post数据时报错:远程服务器返回错误: (400) 错误的请求。

    网上查了多种方法,有不少说法,报400说是传的数据格式不对,最后的结论确实是数据格式不对. Content_Type为:application/json,配的数据格式有些麻烦,特别数多层,单层还好.例 ...

  9. [Swift]LeetCode6. Z字形变换 | ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  10. [Swift]LeetCode322. 零钱兑换 | Coin Change

    You are given coins of different denominations and a total amount of money amount. Write a function ...