列表

详情


NC260. 复杂链表的复制

描述

输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针random指向一个随机节点),请对此链表进行深拷贝,并返回拷贝后的头结点。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)。 下图是一个含有5个结点的复杂链表。图中实线箭头表示next指针,虚线箭头表示random指针。为简单起见,指向null的指针没有画出。

示例:
输入:{1,2,3,4,5,3,5,#,2,#}
输出:{1,2,3,4,5,3,5,#,2,#}
解析:我们将链表分为两段,前半部分{1,2,3,4,5}为ListNode,后半部分{3,5,#,2,#}是随机指针域表示。
以上示例前半部分可以表示链表为的ListNode:1->2->3->4->5
后半部分,3,5,#,2,#分别的表示为
1的位置指向3,2的位置指向5,3的位置指向null,4的位置指向2,5的位置指向null
如下图:

示例1

输入:

{1,2,3,4,5,3,5,#,2,#}

输出:

{1,2,3,4,5,3,5,#,2,#}

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Javascript 解法, 执行用时: 1ms, 内存消耗: 380KB, 提交时间: 2021-11-13

function RandomListNode(x){
    this.label = x;
    this.next = null;
    this.random = null;
}
function Clone(pHead)
{
    // write code here
    //思路:复制节点,赋值random,拆分节点
    if(pHead===null){return null}
    //复制节点
    let temp = pHead
    while(temp){
        let copyNode =new RandomListNode(temp.label)
        copyNode.next = temp.next
        temp.next = copyNode
        temp = temp.next.next
    }
    //赋值random
    temp = pHead
    while(temp){
        if(temp.random){
            temp.next.random = temp.random.next
        } else{temp.next.random = null}
        temp = temp.next.next
    }
    //拆分节点
    let root = pHead.next
    let r = root
    temp = pHead.next
    while(temp){
        if(temp.next===null){break}
        temp = temp.next.next
        r.next = temp
        r = r.next
    }
    //返回新链表头节点
    return root
}
module.exports = {
    Clone : Clone
};

C++ 解法, 执行用时: 2ms, 内存消耗: 356KB, 提交时间: 2021-03-05

/*
struct RandomListNode {
    int label;
    struct RandomListNode *next, *random;
    RandomListNode(int x) :
            label(x), next(NULL), random(NULL) {
    }
};
*/
class Solution {
public:
    RandomListNode* Clone(RandomListNode* pHead) {
       if (!pHead) return NULL;
		RandomListNode *head = pHead;
		while (head)
		{
			RandomListNode *clone= new RandomListNode(head->label);
			//clone->label = head->label;
			clone->next = head->next;
			head->next = clone;
			head = clone->next;
		}

		RandomListNode *head1 = pHead;
		RandomListNode *clone1;
		while (head1)
		{
			clone1 = head1->next;
			if (head1->random != nullptr) clone1->random = head1->random->next;
			head1 = clone1->next;
		}

		RandomListNode *head2 = pHead;
		RandomListNode *clone2;
		RandomListNode *cloned = pHead->next;
		while (head2)
		{
			clone2 = head2->next;
			head2->next = clone2->next;
			head2 = head2->next;
			if(head2) clone2->next = head2->next;
		}
		return cloned;
    }
};

C++ 解法, 执行用时: 2ms, 内存消耗: 356KB, 提交时间: 2021-03-03

/*
struct RandomListNode {
    int label;
    struct RandomListNode *next, *random;
    RandomListNode(int x) :
            label(x), next(NULL), random(NULL) {
    }
};
*/
class Solution {
public:
    struct INFO{
        RandomListNode *ori_now;
        RandomListNode *res_now;
    };
    
    RandomListNode* Clone(RandomListNode* pHead) {
        if( !pHead ) return NULL;
        
        INFO info_arr[88];   int idx = 1;
        RandomListNode *res = new RandomListNode( pHead->label ), *Last = res;
        info_arr[0].ori_now = pHead;
        info_arr[0].res_now = res;
        for(pHead=pHead->next; pHead; pHead=pHead->next){
            Last->next = new RandomListNode( pHead->label );
            Last = Last->next;
            info_arr[idx].ori_now = pHead;
            info_arr[ idx++ ].res_now = Last;
        }
        int i, j;
        for(i=0; i<idx; i++){
            Last = info_arr[i].ori_now->random;
            for(j=0; j<idx; j++)
                if(info_arr[j].ori_now == Last){
                    info_arr[i].res_now->random = info_arr[j].res_now;
                    break;
                }
        }
        return res;
    }
};

C++ 解法, 执行用时: 2ms, 内存消耗: 360KB, 提交时间: 2021-06-07

/*
struct RandomListNode {
    int label;
    struct RandomListNode *next, *random;
    RandomListNode(int x) :
            label(x), next(NULL), random(NULL) {
    }
};
*/
class Solution {
public:
    RandomListNode* Clone(RandomListNode* pHead) {
        if(!pHead)
            return nullptr;
        RandomListNode* pHead1 = pHead;
        while(pHead1 != nullptr){
            RandomListNode* pclone = new RandomListNode(pHead1 -> label);
            //pclone -> label = pHead1 -> label;
            pclone -> next = pHead1 -> next;
            pHead1 -> next = pclone;
            //pclone -> random = pHead1 -> random;
            pHead1 = pclone -> next;
        }
         RandomListNode *head1 = pHead;
        RandomListNode *clone1;
        while (head1)
        {
            clone1 = head1->next;
            if (head1->random != nullptr) clone1->random = head1->random->next;
            head1 = clone1->next;
        }
 
        RandomListNode *head2 = pHead;
        RandomListNode *clone2;
        RandomListNode *cloned = pHead->next;
        while (head2)
        {
            clone2 = head2->next;
            head2->next = clone2->next;
            head2 = head2->next;
            if(head2) clone2->next = head2->next;
        }
        return cloned; 
    }
};

C++ 解法, 执行用时: 2ms, 内存消耗: 360KB, 提交时间: 2021-05-24

/*
struct RandomListNode {
    int label;
    struct RandomListNode *next, *random;
    RandomListNode(int x) :
            label(x), next(NULL), random(NULL) {
    }
};
*/
class Solution {
public:
    RandomListNode* Clone(RandomListNode* pHead) {
        if(pHead==NULL)
            return NULL;
        map<RandomListNode*,RandomListNode*> record;
        RandomListNode* now=pHead;
        while(now!=NULL){
            record.insert(pair<RandomListNode*,RandomListNode*>(now,new RandomListNode(now->label)));
            now = now->next;
        }
        now=pHead;
        RandomListNode* ans=new RandomListNode(pHead->label);
        RandomListNode* p;
        p = ans;
        while(now!=NULL){
            p->next = record[now->next];
            p->random = record[now->random];
            
            now = now->next;
            p = p->next;
        }
        return ans;
    }
};

上一题