一、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. robotframework ride报错 Keyword 'BuiltIn.Log' expected 1 to 5 arguments, got 12.

    错误原因,else和else if使用了小写,必须使用大写才能识别到.

  2. bash: ipconfig: command not found

    问题描述: [root@localhost ~]# ipconfig-bash: ipconfig: command not found[root@localhost ~]# 解决方法一: cd /e ...

  3. tp5.1中的命名规范

    类里边用驼峰(类名首字母大写,属性和方法首字母小写) 类名:User,UserType 类中的属性和方法:getUserName(),tableName,instance 参数.数据表和字段 用小写字 ...

  4. mysql视图创建

    create or replace view partner_v as select a.id,vendorname,address,nation,contactperson,tel1,tel2,jy ...

  5. Vue结合webpack实现路由懒加载和分类打包

    https://blog.csdn.net/weixin_39205240/article/details/80742723

  6. spark数据倾斜与解决方法

    一.数据倾斜 数据倾斜一般发生在对数据进行重新划分以及聚合的处理过程中.执行Spark作业时,数据倾斜一般发生在shuffle过程中,因为Spark的shuffle过程需要进行数据的重新划分处理.在执 ...

  7. XML 属性

    XML 属性 从 HTML,你会回忆起这个:<img src="computer.gif">."src" 属性提供有关 <img> 元素 ...

  8. 论文阅读:Andromeda: Performance, Isolation, and Velocity at Scale in Cloud Network Virtualization (全文翻译用于资料整理和做PPT版本,之后会修改删除)

    Abstract: This paper presents our design and experience with Andromeda,  Google Cloud Platform’s net ...

  9. (51)LINUX应用编程和网络编程之六Linux高级IO

    3.6.1.非阻塞IO 3.6.1.1.阻塞与非阻塞 阻塞:阻塞具有很多优势(是linux系统的默认设置),单路IO的时候使用阻塞式IO没有降低CPU的性能 补充:阻塞/非阻塞, 它们是程序在等待消息 ...

  10. ubuntu彻底删除nginx

    1.先执行一下命令: 1.1 删除nginx,–purge包括配置文件 sudo apt-get --purge remove nginx 1 1.2 自动移除全部不使用的软件包 sudo apt-g ...