Easy to understand C++: using a flag to mark


  • 0
    C
    class Solution {
    public:
        bool hasCycle(ListNode *head) {
            ListNode *sen = new ListNode(-1);
            if (head == NULL) return false;
            while (head->next != NULL) {
                if (head->next == sen) return true;
                ListNode *tmp = head;
                head = head->next;
                tmp->next = sen;
            }
            return false;
        }
    };

Log in to reply
 

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