TLE in {3,2,0,-4}, tail connects to node index 1


  • 0
    W

    Here is my code:

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
        public:
            ListNode *checkCircle(ListNode *h)
        {
            if (h->next==NULL) return NULL;
            if (h->next==h) return h;
            ListNode *tmp=h->next;
            h->next=h;
            return checkCircle(tmp);
        }
    
        ListNode *detectCycle(ListNode *head) {
            if (head==NULL) return NULL;
            return checkCircle(head);
        }
    };
    

    It will change the next to point to itself, and the program will write node when it finds the node which next points to itself. Please help.


  • 2
    S

    Maybe you should not break the original list, I guess, it may lead to judger throw errors.


Log in to reply
 

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