Linked List Cycle C++

class Solution { public: bool hasCycle(ListNode *head) { if (head == NULL){ return false; } else if (head->next == NULL){ return true; } ListNode *slow; ListNode *fast; while((fast!=NULL) && (fast->next!=NULL)){ slow = slow->next; fast = fast->next->next; if (slow == fast){ return true; } } return false; } };

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.