【问题标题】:Linkedlist While loop (python), why we are doing fast.next in while loop instead of fast.next.nextLinkedlist While 循环(python),为什么我们在 while 循环中使用 fast.next 而不是 fast.next.next
【发布时间】:2021-11-03 03:16:52
【问题描述】:
slow = self.head
fast = self.head

while fast and fast.next:
    slow = slow.next
    fast = fast.next.next
 
print("The middle element is ", slow.data)"

中间元素 Fast.next.next> 帮助

【问题讨论】:

标签: python linked-list


【解决方案1】:

while 条件下,我们需要测试fast.next,这样我们就不会在fast.nextNone 时尝试访问fast.next.next

我们不需要在那里测试fast.next.next,因为在我们执行fast = fast.next.next 之后,这将在下一次迭代中测试。

【讨论】:

  • 非常感谢您的解释。
猜你喜欢
  • 1970-01-01
  • 2011-04-21
  • 1970-01-01
  • 2012-11-07
  • 2022-12-12
  • 2018-07-30
  • 1970-01-01
  • 2014-11-02
  • 2020-08-14
相关资源
最近更新 更多