What happened to chk?# Stock
g*v
1 楼
我的代码在我自己机器上没问题,但leetcode上连最简单的case都通不过。
比如 [-1, #] 都没通过。
实在是找不出问题,请大家看看。
----------------------------------------------------------------------------
------------------
RandomListNode *copyRandomList(RandomListNode *head) {
if(head == NULL) return NULL;
RandomListNode *copyhead = new RandomListNode(head->label);
copyhead->random = head->random;
RandomListNode *ori = head->next;
RandomListNode *oritemp;
head->next = copyhead;
RandomListNode *copy = copyhead;
//copy to a new list "copyhead"
while(ori != NULL)
{
RandomListNode *copytemp = new RandomListNode(ori->label);
copytemp->random = ori->random;
copy->next = copytemp;;
oritemp = ori->next;
ori->next = copytemp;
ori = oritemp;
copy = copy->next;
}
//assign the random pointers
copy = copyhead;
while(copy != NULL)
{
if(copy->random != NULL)
copy->random = copy->random->next;
copy = copy->next;
}
return copyhead;
}
比如 [-1, #] 都没通过。
实在是找不出问题,请大家看看。
----------------------------------------------------------------------------
------------------
RandomListNode *copyRandomList(RandomListNode *head) {
if(head == NULL) return NULL;
RandomListNode *copyhead = new RandomListNode(head->label);
copyhead->random = head->random;
RandomListNode *ori = head->next;
RandomListNode *oritemp;
head->next = copyhead;
RandomListNode *copy = copyhead;
//copy to a new list "copyhead"
while(ori != NULL)
{
RandomListNode *copytemp = new RandomListNode(ori->label);
copytemp->random = ori->random;
copy->next = copytemp;;
oritemp = ori->next;
ori->next = copytemp;
ori = oritemp;
copy = copy->next;
}
//assign the random pointers
copy = copyhead;
while(copy != NULL)
{
if(copy->random != NULL)
copy->random = copy->random->next;
copy = copy->next;
}
return copyhead;
}