case1:
if not head or not head.next:
return head
slow = head
fast = head
# 用快慢指针分成两部分
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next

case2
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next

本文介绍了使用快慢指针法来查找链表的中间节点,通过两个案例详细展示了算法的具体实现过程,包括初始化指针、迭代移动直到找到中间节点。
1509

被折叠的 条评论
为什么被折叠?



