题目链接 | 876. 链表的中间结点 |
---|---|
思路 | 快慢指针-经典应用题 |
题解链接 | 没想明白?一个视频讲透!(Python/Java/C++/Go/JS/Rust) |
关键点 | while fast and fast.next: ... |
时间复杂度 | \(O(n)\) |
空间复杂度 | \(O(1)\) |
代码实现:
class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow = fast = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
return slow
标签:结点,slow,876,fast,next,链表
From: https://www.cnblogs.com/WrRan/p/18409086