日野弥生:勉強しよう

LeetCode 876 - 链表的中间结点

发表于2025年02月02日

#链表 #双指针

快慢指针步伐差一,快指针到底时,慢指针在中间位置。

class Solution:
    def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
        # 头为空直接返回None
        if head == None:
            return head
        slow, fast = (head, head)
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        return slow


        

フラッシュタブ:LeetCode

题目链接:https://leetcode.cn/problems/middle-of-the-linked-list/

上一篇

LeetCode 412 - 最富有客户的资产总量

下一篇

LeetCode 1512 - 好数对的数目