my different idea


  • 0
    W

    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

    0_1506390118424_leecode1.png

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


Log in to reply
 

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