133. Clone Graph && 138. Copy List with Random Pointer

133. Clone Graph

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.

OJ's undirected graph serialization:

Nodes are labeled uniquely.

We use # as a separator for each node, and , as a separator for node label and each neighbor of the node. 

As an example, consider the serialized graph {0,1,2#1,2#2,2}.

The graph has a total of three nodes, and therefore contains three parts as separated by #.

  1. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
  2. Second node is labeled as 1. Connect node 1 to node 2.
  3. Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.

Visually, the graph looks like the following:

       1
      / \
     /   \
    0 --- 2
         / \
         \_/
Recursion solution: 
/**
 * Definition for undirected graph.
 * class UndirectedGraphNode {
 *     int label;
 *     List<UndirectedGraphNode> neighbors;
 *     UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
 * };
 */
public class Solution {
    public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
        if(node == null)
            return null;
        Map<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();        
        return cloneGraph(node, map);
    }
    
    private UndirectedGraphNode cloneGraph(UndirectedGraphNode node,  Map<UndirectedGraphNode, UndirectedGraphNode> map)
    {
        UndirectedGraphNode nodeCopy = new UndirectedGraphNode(node.label);
        map.put(node, nodeCopy);    
        for(UndirectedGraphNode neighbor: node.neighbors) {
            UndirectedGraphNode copy = map.get(neighbor);
            if(copy == null) {
                copy = cloneGraph(neighbor, map);
            }
            nodeCopy.neighbors.add(copy);
        }
        return nodeCopy;
    }
}

 Iterative solution:

/**
 * Definition for undirected graph.
 * class UndirectedGraphNode {
 *     int label;
 *     List<UndirectedGraphNode> neighbors;
 *     UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
 * };
 */
public class Solution {
    public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
        if(node == null)
            return null;
        
        Map<UndirectedGraphNode, UndirectedGraphNode> copiesDict 
                = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();
                
        UndirectedGraphNode nodeCopy = new UndirectedGraphNode(node.label);
        copiesDict.put(node,nodeCopy);
        
        Queue<UndirectedGraphNode> q = new ArrayDeque<UndirectedGraphNode>();
        q.add(node);
        while(!q.isEmpty())
        {
            UndirectedGraphNode n = q.remove();
            UndirectedGraphNode nCopy = copiesDict.get(n);
            for(UndirectedGraphNode nodeToVisit: n.neighbors)
            {
                UndirectedGraphNode copy = copiesDict.get(nodeToVisit);
                if(copy == null)
                {
                    copy = new UndirectedGraphNode(nodeToVisit.label);
                    copiesDict.put(nodeToVisit, copy);
                    q.add(nodeToVisit);
                }
                nCopy.neighbors.add(copy);
            }
        }
        
        
        return nodeCopy;
    }
}

 

138. Copy List with Random Pointer

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.

 
/**
 * 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) {
        if(head == null)
            return null;
        Map<RandomListNode, RandomListNode> m = new HashMap<RandomListNode, RandomListNode>();
        return copyRandomList(m, head);
    }
    
    private RandomListNode copyRandomList(Map<RandomListNode, RandomListNode> map, RandomListNode node) {
        RandomListNode copy = new RandomListNode(node.label);
        map.put(node, copy);
        
        if(node.next != null){
            RandomListNode nextCopy = map.get(node.next);
            if(nextCopy == null){
                nextCopy = copyRandomList(map, node.next);
            }
            copy.next = nextCopy;
        }
        
        if(node.random != null){
            RandomListNode randomCopy = map.get(node.random);
            if(randomCopy == null){
                randomCopy = copyRandomList(map, node.random);
            }
            copy.random = randomCopy;
        }
        
        return copy;
    }
}

 

 
 

转载于:https://www.cnblogs.com/neweracoding/p/5244681.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值