一、21合并两个有序链表

代码如下:

class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
# 首先对特殊情况进行处理,l1 l2 为空的时候
if not (l1 and l2) : # 这个表达式的意思只要l1 l2 不全为真就符合条件
return l1 or l2
elif l1.val > l2.val: # 判断哪一个值比较小,然后保留哪一项
l2.next = self.mergeTwoLists(l2.next,l1) # 递归思想的精髓
return l2 # 返回小的这一项
else :
l1.next = self.mergeTwoLists(l1.next,l2)
return l1
# 注意:l1 l2 表示的不是整个链表,而只是每一个结点 # 这个是别人给的题解,大佬写的很好。
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
if l1 and l2: # 判断l1 是否为空
if l1.val > l2.val : l1,l2 = l2,l1 # 始终让l1 为小值
l1.next = self.mergeTwoLists(l1.next,l2) # 递归的精髓
return l1 or l2

二、83删除链表重复的元素

class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
if not head :return head # 首先判断链表是否为空,若为空直接返回
head1 = head
l = [] # 定义一个空的列表 用来存放已经遍历过的链表结点
l.append(head.val) # 将遍历过的链表结点添加进入列表中
while head and head.next: # 当前结点和下一个结点都为真继续
if head.next.val in l : # 若下一节点的值已经在列表中
head.next = head.next.next # 进行删除操作,跳过下一节点,直接指向下下结点
else:
l.append(head.next.val) # 将结点添加进入列表
head = head.next # 进行遍历
return head1 # 返回删除重复元素后的链表

三、141环形链表

# 解题思路;利用快慢指针,一个指针快,另一个慢,若链表有环,则总有相遇的时候
class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
if not head or not head.next : # 若链表中没有元素,或者只有一个元素,则不可能成环
return False
fast = head
slow = head
while fast:
if fast.next == None: # 若快指针可以走到头,则肯定没有环
return False
else :
fast = fast.next.next # 快指针每次走两步
slow = slow.next # 慢指针走一步
if slow == fast: # 当快慢指针相遇
return True
return False # 当快指针为None时退出

四、160相交链表

class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
if not headA or not headB: # 若两个链表中其中一个链表为空则不可能相交
return None
length1 = 0 # 用来接收headA链表的长度
length2 = 0
h1 = headA
h2 = headB
while h1: # 遍历链表算出两个链表的长度
length1 += 1
h1 = h1.next
while h2:
length2 += 1
h2 = h2.next
if length1 > length2: # 让比较长的链表先走
for i in range(length1 - length2):
headA = headA.next
elif length1 < length2:
for i in range(length2 - length1):
headB = headB.next
while headA: # 现在两个指针处于同一位置
if headA == headB: # 判断是否为同一节点
return headA
headA = headA.next
headB = headB.next
return None

五、203移除链表元素

class Solution(object):
def removeElements(self, head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
if not head:return head # 判断是否为空链表
head1 = head # 用head1返回新的链表
while head: # 找到第一个不需要删除的链表
if head.val == val: # 需要删除时,将head1等于head的下一节点
head1 = head.next
head = head.next
else:
break
while head and head.next: # 进行链表的遍历,进行删除所给的元素
if head.next.val == val:
head.next = head.next.next
else :
head = head.next
return head1 # 返回新的链表

六、234回文链表

# 用快慢指针的方法找到链表的中间节点,然后将后半段链表进行反转,在判断
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
if not head or not head.next : # 若链表为空或为一个元素,则链表为回文链表
return True
fast = head
slow = head # 快慢指针 快指针走两步,慢指针走一步
while fast and fast.next:
fast = fast.next.next
slow = slow.next
mid = slow # 此时slow为中点指针
pre = None
# 将后半段链表表反转
while slow:
slow.next,pre,slow = pre,slow,slow.next
# 不可以分开写成三个语句,Python特有,在C语言中这样写是错误的
# 他们这三个语句的执行是相同,没有先后之分。
# 此时后半段列表的头指针为pre
while head != mid:
if head.val != pre.val:
return False
pre = pre.next
head = head.next
return True

LeetCode链表简单题的更多相关文章

  1. leetcode 链表类型题总结

    链表测试框架示例: // leetcodeList.cpp : 定义控制台应用程序的入口点.vs2013 测试通过 // #include "stdafx.h" #include ...

  2. leetcode 63 简单题

    题目很水... 直接放代码了 int uniquePathsWithObstacles(int** obstacleGrid, int obstacleGridRowSize, int obstacl ...

  3. 这样leetcode简单题都更完了

    这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...

  4. leetcode简单题6

    今天的华师 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...

  5. LeetCode 75,90%的人想不出最佳解的简单题

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题的44篇文章,我们一起来看下LeetCode的75题,颜色排序 Sort Colors. 这题的官方难度是Medi ...

  6. 链表算法题之中等级别,debug调试更简单

    文章简述 大家好,本篇是个人的第 5 篇文章 从本篇文章开始,分享关于链表的题目为中等难度,本次共有 3 道题目. 一,两数相加 1.1 题目分析 题中写到数字是按照逆序的方式存储,从进位的角度看,两 ...

  7. 【python】Leetcode每日一题-旋转链表

    [python]Leetcode每日一题-旋转链表 [题目描述] 给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置. 示例1: 输入:head = [1,2,3,4,5] ...

  8. 【python】Leetcode每日一题-删除排序链表中的重复元素

    [python]Leetcode每日一题-删除排序链表中的重复元素 [题目描述] 存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除所有重复的元素,使每个元素 只出现一次 . 返回同 ...

  9. 【python】Leetcode每日一题-删除排序链表中的重复元素2

    [python]Leetcode每日一题-删除排序链表中的重复元素2 [题目描述] 存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表 ...

随机推荐

  1. [SCOI2016]幸运数字(线性基,倍增)

    [SCOI2016]幸运数字 题目描述 A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个幸运数字,以纪念碑的形式矗立在这座城市的正中心,作 ...

  2. Java并发编程实战 第10章 避免活跃性危险

    死锁 经典的死锁:哲学家进餐问题.5个哲学家 5个筷子 如果没有哲学家都占了一个筷子 互相等待筷子 陷入死锁 数据库设计系统中一般有死锁检测,通过在表示等待关系的有向图中搜索循环来实现. JVM没有死 ...

  3. 流式布局和viewport

    流式布局 百分比布局,非固定宽度,内容向两边填充,流动的布局. viewport(视口) PC端的网页在手机端的浏览器显示是不会出现网页的,这是因为移动端的网页不是直接放在移动端的浏览器中,而是放在移 ...

  4. 详解WebService开发中四个常见问题(1)

    详解WebService开发中四个常见问题(1)   WebService开发中经常会碰到诸如WebService与方法重载.循环引用.数据被穿该等等问题.本文会给大家一些很好的解决方法. AD:WO ...

  5. percona-toolkit 3.0.13 简单安装记录

    percona-toolkit 3.0.13 简单安装记录 环境:centos6.x mysql:8.0.17 yum -y install perl-DBIyum -y install perl-D ...

  6. jvm——CMS 垃圾回收器(未完)

    https://matt33.com/2018/07/28/jvm-cms/ 阶段1:Initial Mark stop-the-wolrd 标记那些直接被 GC root 引用或者被年轻代存活对象所 ...

  7. 引用自定义的css或者js文件

    用script标签,src是js文件路径 <script type="text/javascript" src="./js/udc.js">< ...

  8. toJSON() 方法,将 Date 对象转换为字符串,并格式化为 JSON 数据格式。

    JavaScript toJSON() 方法 定义和用法 toJSON() 方法可以将 Date 对象转换为字符串,并格式化为 JSON 数据格式. JSON 数据用同样的格式就像x ISO-8601 ...

  9. 【POJ2992】Divisors

    [题目概括] 计算\(C_n^k\)的因子个数. [思路要点] 首先考虑将组合数展开,展开后就是\(\frac {n!}{k!\times (n-k)!}\). 这样就是计算出这些质因子的个数,然后将 ...

  10. 【技术分享:python 应用之二】解锁用 VSCode 写 python 的正确姿势

    之前一直用 notepad++ 作为编辑器,偶然发现了 VScode 便被它的颜值吸引.用过之后发现它启动快速,插件丰富,下载安装后几乎不用怎么配置就可以直接使用,而且还支持 markdown.当然, ...