[leetcode-138]Copy List with Random Pointer(java)

问题描述:
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

Return a deep copy of the list.

分析:这题和clone graph可以说是异曲同工,同样使用一个map来对旧的节点与新的节点进行映射。然后再遍历一遍list,获取对应的新的节点图。

代码如下:440ms

/**
 * Definition for singly-linked list with a random pointer.
 * class RandomListNode {
 *     int label;
 *     RandomListNode next, random;
 *     RandomListNode(int x) { this.label = x; }
 * };
 */
public class Solution {
    public RandomListNode copyRandomList(RandomListNode head) {
        HashMap<RandomListNode,RandomListNode> maps = new HashMap<>();
        Queue<RandomListNode> queue = new LinkedList<>();

        RandomListNode curNode = head;
        while (curNode!=null){
            maps.put(curNode,new RandomListNode(curNode.label));
            curNode = curNode.next;
        }
        curNode = head;
        while(curNode!=null){
            maps.get(curNode).next = maps.get(curNode.next);
            if(curNode.random!=null)
                maps.get(curNode).random = maps.get(curNode.random);
            else
                maps.get(curNode).random = null;
            curNode = curNode.next;
        }
        return maps.get(head);
    }
}

补充:看到下面一张图,突然觉得太奇妙了
这里写图片描述
首先,要经过三次遍历,第一次遍历,将新节点插入到原有的list中
第二次遍历,利用如下公式,生成random信息
newNode ->random = oldNode->random->next;
第三次遍历,将原有的链表分开为两个链表。

代码如下:128ms

/**
 * Definition for singly-linked list with a random pointer.
 * struct RandomListNode {
 *     int label;
 *     RandomListNode *next, *random;
 *     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
 * };
 */
class Solution {
public:
    RandomListNode *copyRandomList(RandomListNode *head) {
        if (!head)
            return NULL;

        RandomListNode *curNode = head;
        RandomListNode *nextNode;

        while (curNode) {
            nextNode = curNode->next;
            RandomListNode *newNode = new RandomListNode(curNode->label);
            curNode->next = newNode;
            newNode->next = nextNode;
            curNode = nextNode;
        }
        //deal with the random list
        curNode = head;
        while(curNode && curNode->next){
            if(curNode->random)
                curNode->next->random = curNode->random->next;
            curNode = curNode->next->next;
        }
        RandomListNode *newHead = head->next;
        curNode = head;
        //恢复oldlist and newList
        while (curNode && curNode->next != NULL) {
            nextNode = curNode->next;
            curNode->next = nextNode->next;
            curNode = nextNode;
        }
        return newHead;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值