My Solution,I think it's interesting!


  • 1
    B
    class Solution {
    public:
        bool hasCycle(ListNode *head) {
            ListNode *p,*q;
            p=head;
            if(p==NULL) return false;
            if(p->next==NULL) return false;
            while(p->next!=NULL){
                q=p->next;
                p->next=p;
                if(p==q) return true;
                else p=q;
            }
            return false;
        }
    };

  • 0
    O

    Hmm... Good solution, although very difficult to read. But did you just broke the structure of the original link list?


  • 0
    Y

    Yes the original linked list structure was broken.
    I think we should detect cycle without breaking the linked list structure...


Log in to reply
 

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