Why did I get "time limit exceeded" for input {-1,-1}?


  • 0
    W
    class Solution {
    public:
        RandomListNode *copyRandomList(RandomListNode *head) {
            if(!head) return head;
            RandomListNode* p = head;
            while(p){
                RandomListNode* node = new RandomListNode(p->label);
                node->next = p->next;
                p->next = node;
                p = node->next;
            }
            
            p = head;
            while(p){
                if(!(p->random)){
                    p = p->next->next;
                    continue;
                }
                p->next->random = p->random->next;
            }
            
            p = head;
            RandomListNode* copy = p->next;
            RandomListNode* q;
            while(p){
                q = p->next;
                p = q->next;
                if(p) q->next = p->next;
                else q->next = NULL;
            }
            return copy;
        }
    };

Log in to reply
 

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