Accept~( no other extra space)


  • 0
    J
    class Solution {
    
    public:
    bool hasCycle(ListNode *head) {
    	if(head==NULL)
    		return false;
    	while(head!=NULL)
    	{
    		head->val=-101;
    		head=head->next;
    		
    		if(head!=NULL && head->val == -101)
    		{
    			return true;
    		}
    	}
    
    	return false;
    }
    };

  • 0
    H

    This lucky number -101 can not be generalised. If you try head->val=10; you will get wrong answer.


  • 0
    J

    Yes, you are right. It is just a special flag to determine whether the node is visited. And head->val=10,the number 10 may be appeared frequently in the test cases.


  • 0
    Z

    yes. no one said the int value cannot be negative. they can also repeat


Log in to reply
 

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