【2025-02-28】基础算法:快慢指针

📝前言说明:
●本专栏主要记录本人的基础算法学习以及LeetCode刷题记录,主要跟随B站博主灵茶山的视频进行学习,专栏中的每一篇文章对应B站博主灵茶山的一个视频
●题目主要为B站视频内涉及的题目以及B站视频中提到的“课后作业”。
●文章中的理解仅为个人理解。
●文章中的截图来源于B站博主灵茶山,如有侵权请告知。

🎬个人简介:努力学习ing
📋本专栏:python刷题专栏
📋其他专栏:C语言入门基础python入门基础Linux操作系统基础
🎀CSDN主页 愚润泽

一,视频题目

876. 链表的中间结点

在这里插入图片描述

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        return slow

141. 环形链表

在这里插入图片描述

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        # 慢的一定会追上快的
        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next # 因为这里要对fast.next操作,所以循环条件里面也需要确保fast.next 不为空
            if fast == slow:
                return True
        return False

142.环形链表 II

在这里插入图片描述
在这里插入图片描述

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if slow == fast: # 相遇了
                while slow != head:
                    head = head.next
                    slow = slow.next
                return slow
        return None

143. 重排链表

在这里插入图片描述

把问题看成:
1,先找到中间节点,链表分成两个链表
2,把后半段链表反转
3,把后半段链表插入第一段链表
在这里插入图片描述

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def findMid(self, head): # 若链表长度为偶数:第一段应该会比第二段多一个元素(第一段的末尾还会指向mid元素)
        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        return slow

    def reverseList(self, head):
        pre = None
        cur = head
        while cur:
            nxt = cur.next
            cur.next = pre
            pre = cur
            cur = nxt
        return pre # 返回反转后的链表的头结点

    def reorderList(self, head: Optional[ListNode]) -> None:
        """
        Do not return anything, modify head in-place instead.
        """
        mid = self.findMid(head)
        head2 = self.reverseList(mid)
        while head2.next:
            nxt_head1 = head.next
            nxt_head2 = head2.next
            head.next = head2
            head2.next = nxt_head1
            head = nxt_head1
            head2 = nxt_head2

二,课后作业

234. 回文链表

在这里插入图片描述

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def Mid(self,head):
        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        return slow

    def reverseList(slef, head):
        pre = None
        cur = head
        while cur:
            nxt = cur.next
            cur.next = pre
            pre = cur
            cur = nxt
        return pre # 最后pre指向原链表的最后一个节点
    def isPalindrome(self, head: Optional[ListNode]) -> bool:
        # 反转 + 逐个判断
        # 1,先找到中间节点;2, 反转后半部分3, 再逐个比较
        head2 = self.Mid(head)
        head2 = self.reverseList(head2)
        # head2 从最往前,head从前往后
        while head2:
            if head2.val != head.val:
                return False
            head = head.next
            head2 = head2.next
        return True 

2130. 链表最大孪生和

在这里插入图片描述

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def Mid(self,head):
        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        return slow

    def reverseList(slef, head):
        pre = None
        cur = head
        while cur:
            nxt = cur.next
            cur.next = pre
            pre = cur
            cur = nxt
        return pre

    def pairSum(self, head: Optional[ListNode]) -> int:
        # n-1-i 其实就是从后往前数
        head2 = self.Mid(head)
        head2 = self.reverseList(head2)
        mx = 0
        while head and head2:
            s = head.val + head2.val
            if s > mx:
                mx = s
            head = head.next
            head2 = head2.next
        return mx
        

总结:
1,快慢指针可以用于找中间节点
2,快慢指针可以用于相遇问题,然后通过数学推导分析移动路程关系
3,对于对称型列表,可以先找到中间节点,然后反转后半段链表
注意:找到中间节点后反转的链表,第一段在链表元素为偶数时,比第二段长(藕断丝连)

🌈我的分享也就到此结束啦🌈
要是我的分享也能对你的学习起到帮助,那简直是太酷啦!
若有不足,还请大家多多指正,我们一起学习交流!
📢公主,王子:点赞👍→收藏⭐→关注🔍
感谢大家的观看和支持!祝大家都能得偿所愿,天天开心!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

愚润泽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值