my different idea

  • 0

    reverse the linked list
    if it has a circle, the head->next will point to nullptr
    else it point to next node
    maybe this method would destroy the linked list,but
    we can reverse the linked list again to make it recover


    bool hasCycle(ListNode head) {
    if (head == NULL || head->next == nullptr)
    return false;
    left = head, temp;
    right = head->next;
    left->next = nullptr;
    while (right) {
    temp = right->next;
    right->next = left;
    left = right;
    right = temp;
    if( head->next==nullptr)
    return false;
    return true;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.