[Python] Why am I getting "hascycle" error for this question?

Discussão em 'Python' iniciado por Stack, Abril 19, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to solve a leetcode problem where we have to find if a cycle exists in a LinkedList.

    This is the code that I tried but I am getting an error which says I have a cycle in the list. What am I doing wrong?

    class Solution:
    def hasCycle(self, head: Optional[ListNode]) -> bool:

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

    if slow is fast:
    return True
    return False


    This is the error I am getting:

    TypeError: cannot unpack non-iterable ListNode object
    ^^^^^^^^^
    slow,fast=head
    Line 10 in hasCycle (Solution.py)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ret = Solution().hasCycle(param_1)
    Line 40 in _driver (Solution.py)
    _driver()
    Line 53 in <module> (Solution.py)

    Continue reading...

Compartilhe esta Página