日野弥生:勉強しよう
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