Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
题目解析:
(1)如果有指针的值是NULL,那就肯定有环了
(2)将经过的链表的节点赋值为某个特定的数字,当循环以后再遇到这个数字的时候就说明有环了啊
bool hasCycle(ListNode *head) {
while(head!=NULL)
{
if(head->val != INT_MAX)
head->val = INT_MAX;
else
return true;
head = head->next;
}
if(head == NULL)
return false;
}