导航页-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. 32位二进制IP地址与十进制IP地址互相转换

    代码: import java.util.List; import java.util.ArrayList; import java.util.Scanner; public class Transf ...

  2. C#调用Interrop.excel导出Excel文件失败解决方案

    最近操作员反馈系统在导出Excel时失败,有抛出如下异常:系统错误信息:检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件时失 ...

  3. 我理解的websocket

    短轮询:客户端发起请求,服务器无论有无消息都返回信息,结束http连接.然后继续发起请求. 长轮询:客户端发起请求,建立连接,直到服务端返回消息response,结束http连接.然后继续发起请求,重 ...

  4. Kiwi Syslog安装笔记

    Kiwi Syslog是一款比较好用日志服务器,采集设备.服务器日志简单又方便,近日试架成功,记录重点部分. 1. 网络设备配置 举例思科: R1#configure t R1(config)#log ...

  5. org.hibernate.hql.internal.ast.QuerySyntaxException: XXX is not mapped

    异常情况: 最近在把一个项目拆分多个 module 的时候数据库查询遇到这个异常:org.hibernate.hql.internal.ast.QuerySyntaxException: Identi ...

  6. Sqoop葵花宝典

    Sqoop葵花宝典 基于Sqoop1.x 场景 导入流程 graph LR A[RDBMS] -->|Sqoop| B(Hive) 导出流程 graph LR A[Hive] -->|Sq ...

  7. [LeetCode] Backspace String Compare 退格字符串比较

    Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...

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

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

  9. swust oj 1011

    二叉排序树的实现和查找 1000(ms) 10000(kb) 2782 / 6301 按照给定的关键字集合,建立二叉排序树.在建立的二叉排序树上查找指定的关键字,查找成功,输出找到该关键字比较的次数: ...

  10. 手机touch事件及参数【转】(自己懒得写了,找了一篇摘过来)

    [html5构建触屏网站]之touch事件 前言 一个触屏网站到底和传统的pc端网站有什么区别呢,交互方式的改变首当其冲.例如我们常用的click事件,在触屏设备下是如此无力. 手机上的大部分交互都是 ...