Copy List with Random Pointer

题目大意:复制一个具有随机指针的链表。也就是说一个单链表中的每一个节点除了有next域之外还有一个random域,这个域指向这个单链表中的任意一个节点,当然也可以不指向任何节点,让你复制这个链表。

思路分析:遍历第一遍先将单链表复制出来,第二遍遍历时再更新random域,在第一遍遍历的过程当中保存旧链表节点和新链表节点之间的映射关系,以旧链表的节点为key,以相对应的新链表节点为value。

代码如下:

/**
 * 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) {
        RandomListNode oldCurrent, newCurrent, newHead, tmpNode;
        if(head==null) {
            return null;
        }
        oldCurrent = head;
        Map<RandomListNode, RandomListNode> oldNode2newNodeMap = new HashMap<RandomListNode, RandomListNode>();
        newHead = new RandomListNode(oldCurrent.label);
        newCurrent = newHead;
        oldNode2newNodeMap.put(oldCurrent, newCurrent);
        oldCurrent = oldCurrent.next;
        while(oldCurrent!=null) {
            tmpNode = new RandomListNode(oldCurrent.label);
            newCurrent.next = tmpNode;
            newCurrent = tmpNode;
            oldNode2newNodeMap.put(oldCurrent, newCurrent);
            oldCurrent = oldCurrent.next;
        }
        oldCurrent = head;
        newCurrent = newHead;
        while(oldCurrent!=null) {
            if(oldCurrent.random!=null) {
                newCurrent.random = oldNode2newNodeMap.get(oldCurrent.random);
            }
            else {
                newCurrent.random = null;
            }
            oldCurrent = oldCurrent.next;
            newCurrent = newCurrent.next;
        }
        return newHead;
    }
}

思路比较清晰,但是代码略显繁杂,还有一种思路可以简化一下。那就是不管什么单链表不单链表,第一遍遍历的时候只把那些节点建立出来,并且同样建立map映射关系,在第二遍遍历的时候才更新next域和random域。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值