【题目】
【代码】
【方法1】双指针——快慢指针

class Solution:
def middleNode(self, head: ListNode) -> ListNode:
fast,slow=head,head
while fast:
fast=fast.next
if fast:
fast=fast.next
slow=slow.next
return slow
本文介绍了一种使用快慢指针的方法来寻找链表的中间节点。通过两个指针,一个快指针每次移动两步,一个慢指针每次移动一步,在快指针到达链表尾部时,慢指针正好位于链表的中间位置。

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



