You are viewing a single comment's thread. Return to all comments →
/* Detect a cycle in a linked list. Note that the head pointer may be 'NULL' if the list is empty.
A Node is defined as: struct Node { int data; struct Node* next; } */
bool has_cycle(Node* head) { // Complete this function // Do not write the main method if ( NULL == head ) return false;
Node *temp = head; Node **visit = new Node *[200]; int num=0; while (temp ) { for (int i=0; i<num; i++) { if ( visit[i] == temp ) { delete [] visit; return true; } } visit[num++] =temp; temp = temp->next; } return false;
}
Linked Lists: Detect a Cycle
You are viewing a single comment's thread. Return to all comments →
/* Detect a cycle in a linked list. Note that the head pointer may be 'NULL' if the list is empty.
A Node is defined as: struct Node { int data; struct Node* next; } */
bool has_cycle(Node* head) { // Complete this function // Do not write the main method if ( NULL == head ) return false;
}