class Solution {
public:
    RandomListNode* Clone(RandomListNode* pHead) {
        if(pHead == NULL) return {};
        RandomListNode* p = pHead;
        RandomListNode* res = new RandomListNode(p->label);  //初始化目标链表
        p = p->next;
        RandomListNode* rec = res;
        while(p){   //把原链表复制到目标链表
            RandomListNode* temp = new RandomListNode(p->label);
            res->next = temp;
            res = res->next;
            p = p->next;
        }
        p = pHead;
        res = rec;
        while(p){
            RandomListNode* temp = pHead;
            int count = 0;   //count用来记录random是开头的第几个
            if(p->random == NULL){    //如果random指空直接下次就行
                res->random = NULL;
                p = p->next;
                res = res->next;
                continue;
            }
            while(temp != p->random){   //通过temp找到原链表的random指向谁,记录为count
                temp = temp->next;
                count++;
            }
            temp = rec;  //temp等于目标链表头
            while(count--){   //通过count找到random在目标链表的哪个位置
                temp = temp->next;
            }
            res->random = temp;   //random指过去即可
            p = p->next;
            res = res->next;
        }
        return rec;
    }
};