Dreaded expected {-1, -1} with output {-1, #}


  • 0
    Y

    Why does the OJ have a problem with my code? It works fine on my machine:

    class Solution {
    public:
        RandomListNode *copyRandomList(RandomListNode *head) {
            if (!head) return NULL;
            map<RandomListNode*, int> pos;
            vector<RandomListNode*> pointers2;
            pos[head] = 0;
            RandomListNode* tmp = head;
            RandomListNode* head2 = new RandomListNode(head->label);
            pointers2.push_back(head2);
            RandomListNode* tmp2 = head2;
            int p = 1;
            if (!head->next) return head2;
            while (tmp->next) {
                pos[tmp->next] = p++;
                tmp2->next = new RandomListNode(tmp->next->label);
                pointers2.push_back(tmp2->next);
                tmp = tmp->next;
                tmp2 = tmp2->next;
            }
            tmp2 = head2;
            tmp = head;
            while (tmp2) {
                if (tmp->random)
                    tmp2->random = pointers2[pos[tmp->random]];
                tmp2 = tmp2->next;
                tmp = tmp->next;
            }
            return head2;
        }
    };

Log in to reply
 

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