zoukankan      html  css  js  c++  java
  • 141环形链表

    # Definition for singly-linked list.
    class ListNode:
    def __init__(self, x):
    self.val = x
    self.next = None
    # 这道题我们用快慢指针的方式可以很容易的就写出来。
    # 定义两个指针,一个指针一次走一步,另一个指针一次走两步,
    # 这样如果链表中有环的话,两个指针总会相遇的。
    class Solution:
    def hasCycle(self, head: ListNode) -> bool:
    fast,slow = head,head
    # 注意这里的条件,一定要slow,fast,写在前面。
    while slow and fast and fast.next:
    fast = fast.next.next
    slow = slow.next
    if fast == slow:
    return True
    else:return False
  • 相关阅读:
    2.8
    2.7
    2.6
    2.5
    2.4第三篇读后感
    2.2第一篇读后感
    2.1
    字符统计
    6468: Snuke's Coloring
    6463: Tak and Hotels II
  • 原文地址:https://www.cnblogs.com/cong12586/p/13784148.html
Copyright © 2011-2022 走看看